Skip to content

Commit

Permalink
Remove macOS specific random bytes impl, with one that might work on …
Browse files Browse the repository at this point in the history
…Linux
  • Loading branch information
Sajjon committed Feb 14, 2024
1 parent 8a592cf commit f16d451
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions tests/uniffi/bindings/test_bag_of_bytes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,18 @@ extension Data {
}
}

func randomByteArray(byteCount count: Int) throws -> [UInt8] {
#if canImport(Darwin) || os(Linux) || os(Android) || os(Windows)
var rng = SystemRandomNumberGenerator()
return (0..<count).map { _ in rng.next() }
#else
fatalError("No secure random number generator on this platform.")
#endif
}

extension Data {
public static func random(byteCount: Int) throws -> Self {
var bytes = [UInt8](repeating: 0, count: byteCount)
let status = SecRandomCopyBytes(kSecRandomDefault, byteCount, &bytes)
if status == errSecSuccess {
return Self(bytes)
}
struct UnableToGenerateBytes: Swift.Error {}
throw UnableToGenerateBytes()
Data(randomBytesArray(byteCount: byteCount))
}
}

Expand Down

0 comments on commit f16d451

Please sign in to comment.