forked from mozilla-mobile/focus-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBytes.swift
49 lines (42 loc) · 1.73 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
49
/* 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.
*/
open class Bytes {
open class func generateRandomBytes(_ len: UInt) -> Data {
let len = Int(len)
var data = Data(count: len)
data.withUnsafeMutableBytes { (p: UnsafeMutablePointer<UInt8>) in
if (SecRandomCopyBytes(kSecRandomDefault, len, p) != errSecSuccess) {
fatalError("Random byte generation failed.")
}
}
return data
}
open class func generateGUID() -> GUID {
// Turns the standard NSData encoding into the URL-safe variant that Sync expects.
return generateRandomBytes(9)
.base64EncodedString(options: NSData.Base64EncodingOptions())
.replacingOccurrences(of: "/", with: "_", options: NSString.CompareOptions(), range: nil)
.replacingOccurrences(of: "+", with: "-", options: NSString.CompareOptions(), range: nil)
}
open class func decodeBase64(_ b64: String) -> Data? {
return Data(base64Encoded: b64,
options: NSData.Base64DecodingOptions())
}
/**
* Turn a string of base64 characters into an NSData *without decoding*.
* This is to allow HMAC to be computed of the raw base64 string.
*/
open class func dataFromBase64(_ b64: String) -> Data? {
return b64.data(using: String.Encoding.ascii, allowLossyConversion: false)
}
func fromHex(_ str: String) -> Data {
// TODO
return Data()
}
}