Skip to content

Commit c03de47

Browse files
committed
Replace SecRandomCopyBytes
Using SystemRandomNumberGenerator instead as it's compatible with Linux and other platforms.
1 parent dcb2cd4 commit c03de47

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

Sources/ATCryptography/SecureRandom.swift

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,16 @@ public struct SecureRandom {
1616
/// - Parameter byteCount: The number of random bytes to generate.
1717
/// - Returns: A securely generated random byte array (`[UInt8]`).
1818
public static func randomBytes(_ byteCount: Int) throws -> [UInt8] {
19-
var bytes = [UInt8](repeating: 0, count: 10)
20-
let status = SecRandomCopyBytes(kSecRandomDefault, bytes.count, &bytes)
21-
22-
guard status == errSecSuccess else { // Always test the status.
23-
throw SecureRandomError.failedToGenerateRandomBytes
19+
guard byteCount > 0 else { return [] }
20+
21+
var randomBytes = [UInt8](repeating: 0, count: byteCount)
22+
var systemRandomNumberGenerator = SystemRandomNumberGenerator()
23+
24+
for i in 0..<byteCount {
25+
randomBytes[i] = UInt8.random(in: 0...255, using: &systemRandomNumberGenerator)
2426
}
25-
26-
return bytes
27+
28+
return randomBytes
2729
}
2830

2931
/// Generates a random `String` with the given encoding.

0 commit comments

Comments
 (0)