|
5 | 5 |
|
6 | 6 | import Foundation |
7 | 7 |
|
8 | | -extension Data { |
| 8 | +public extension Data { |
| 9 | + |
9 | 10 | init<T>(fromArray values: [T]) { |
10 | 11 | let values = values |
11 | 12 | let ptrUB = values.withUnsafeBufferPointer { (ptr: UnsafeBufferPointer) in return ptr } |
@@ -33,32 +34,34 @@ extension Data { |
33 | 34 | return difference == UInt8(0x00) |
34 | 35 | } |
35 | 36 |
|
36 | | - public static func zero(_ data: inout Data) { |
| 37 | + static func zero(_ data: inout Data) { |
37 | 38 | let count = data.count |
38 | 39 | data.withUnsafeMutableBytes { (body: UnsafeMutableRawBufferPointer) in |
39 | 40 | body.baseAddress?.assumingMemoryBound(to: UInt8.self).initialize(repeating: 0, count: count) |
40 | 41 | } |
41 | 42 | } |
42 | 43 |
|
43 | | - public static func randomBytes(length: Int) -> Data? { |
44 | | - for _ in 0...1024 { |
45 | | - var data = Data(repeating: 0, count: length) |
46 | | - let result = data.withUnsafeMutableBytes { (body: UnsafeMutableRawBufferPointer) -> Int32? in |
47 | | - if let bodyAddress = body.baseAddress, body.count > 0 { |
48 | | - let pointer = bodyAddress.assumingMemoryBound(to: UInt8.self) |
49 | | - return SecRandomCopyBytes(kSecRandomDefault, length, pointer) |
50 | | - } else { |
51 | | - return nil |
52 | | - } |
53 | | - } |
54 | | - if let notNilResult = result, notNilResult == errSecSuccess { |
55 | | - return data |
56 | | - } |
| 44 | + /** |
| 45 | + Generates an array of random bytes of the specified length. |
| 46 | + This function uses `SecRandomCopyBytes` to generate random bytes returning it as a `Data` object. |
| 47 | + If an error occurs during random bytes generation, the function returns `nil`. |
| 48 | + Error occurs only if `SecRandomCopyBytes` returns status that is not `errSecSuccess`. |
| 49 | + See [all status codes](https://developer.apple.com/documentation/security/1542001-security_framework_result_codes) for possible error reasons. |
| 50 | + Note: in v4 of web3swift this function will be deprecated and a new implementation will be provided that will throw occurred error. |
| 51 | + - Parameter length: The number of random bytes to generate. |
| 52 | + |
| 53 | + - Returns: optional `Data` object containing the generated random bytes, or `nil` if an error occurred during generation. |
| 54 | + */ |
| 55 | + static func randomBytes(length: Int) -> Data? { |
| 56 | + var entropyBytes = [UInt8](repeating: 0, count: length) |
| 57 | + let status = SecRandomCopyBytes(kSecRandomDefault, entropyBytes.count, &entropyBytes) |
| 58 | + guard status == errSecSuccess else { |
| 59 | + return nil |
57 | 60 | } |
58 | | - return nil |
| 61 | + return Data(entropyBytes) |
59 | 62 | } |
60 | 63 |
|
61 | | - public func bitsInRange(_ startingBit: Int, _ length: Int) -> UInt64? { // return max of 8 bytes for simplicity, non-public |
| 64 | + func bitsInRange(_ startingBit: Int, _ length: Int) -> UInt64? { // return max of 8 bytes for simplicity, non-public |
62 | 65 | if startingBit + length / 8 > self.count, length > 64, startingBit > 0, length >= 1 { return nil } |
63 | 66 | let bytes = self[(startingBit/8) ..< (startingBit+length+7)/8] |
64 | 67 | let padding = Data(repeating: 0, count: 8 - bytes.count) |
|
0 commit comments