forked from mozilla-mobile/firefox-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBytes.swift
48 lines (40 loc) · 1.76 KB
/
Bytes.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
import Foundation
public typealias GUID = String
/**
* Utilities for futzing with bytes and such.
*/
public class Bytes {
public class func generateRandomBytes(len: UInt) -> NSData {
let len = Int(len)
let data: NSMutableData! = NSMutableData(length: len)
let bytes = UnsafeMutablePointer<UInt8>(data.mutableBytes)
let result: Int32 = SecRandomCopyBytes(kSecRandomDefault, len, bytes)
assert(result == 0, "Random byte generation failed.");
return data
}
public class func generateGUID() -> GUID {
// Turns the standard NSData encoding into the URL-safe variant that Sync expects.
return generateRandomBytes(9)
.base64EncodedStringWithOptions(NSDataBase64EncodingOptions())
.stringByReplacingOccurrencesOfString("/", withString: "_", options: NSStringCompareOptions(), range: nil)
.stringByReplacingOccurrencesOfString("+", withString: "-", options: NSStringCompareOptions(), range: nil)
}
public class func decodeBase64(b64: String) -> NSData {
return NSData(base64EncodedString: b64,
options: NSDataBase64DecodingOptions())!
}
/**
* Turn a string of base64 characters into an NSData *without decoding*.
* This is to allow HMAC to be computed of the raw base64 string.
*/
public class func dataFromBase64(b64: String) -> NSData? {
return b64.dataUsingEncoding(NSASCIIStringEncoding, allowLossyConversion: false)
}
func fromHex(str: String) -> NSData {
// TODO
return NSData()
}
}