File tree Expand file tree Collapse file tree 1 file changed +9
-7
lines changed Expand file tree Collapse file tree 1 file changed +9
-7
lines changed Original file line number Diff line number Diff line change @@ -16,14 +16,16 @@ public struct SecureRandom {
16
16
/// - Parameter byteCount: The number of random bytes to generate.
17
17
/// - Returns: A securely generated random byte array (`[UInt8]`).
18
18
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)
24
26
}
25
-
26
- return bytes
27
+
28
+ return randomBytes
27
29
}
28
30
29
31
/// Generates a random `String` with the given encoding.
You can’t perform that action at this time.
0 commit comments