Skip to content

Commit 7765607

Browse files
committed
remove SecRandom
1 parent a2b3967 commit 7765607

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
extension FixedWidthInteger {
2+
public static func random() -> Self {
3+
return Self.random(in: .min ... .max)
4+
}
5+
6+
public static func random<T>(using generator: inout T) -> Self
7+
where T : RandomNumberGenerator
8+
{
9+
return Self.random(in: .min ... .max, using: &generator)
10+
}
11+
}
12+
13+
extension Array where Element: FixedWidthInteger {
14+
public static func random(count: Int) -> [Element] {
15+
var array: [Element] = .init(repeating: 0, count: count)
16+
(0..<count).forEach { array[$0] = Element.random() }
17+
return array
18+
}
19+
20+
public static func random<T>(count: Int, using generator: inout T) -> [Element]
21+
where T: RandomNumberGenerator
22+
{
23+
var array: [Element] = .init(repeating: 0, count: count)
24+
(0..<count).forEach { array[$0] = Element.random(using: &generator) }
25+
return array
26+
}
27+
}

Sources/WebAuthn/WebAuthnManager.swift

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -242,9 +242,6 @@ extension WebAuthnManager {
242242
/// - Throws: An error if something went wrong while generating random byte
243243
/// - Returns: 32 bytes
244244
public func generateChallengeString() throws -> [UInt8] {
245-
var bytes = [UInt8](repeating: 0, count: 32)
246-
let status = SecRandomCopyBytes(kSecRandomDefault, bytes.count, &bytes)
247-
guard status == errSecSuccess else { throw WebAuthnManagerError.challengeGenerationFailed }
248-
return bytes
245+
[UInt8].random(count: 32)
249246
}
250247
}

0 commit comments

Comments
 (0)