8000 Replace SecCopyRandomBytes with Swift 4.2+ random · tmthecoder/Argon2Swift@9ed386c · GitHub
[go: up one dir, main page]

Skip to content

Commit 9ed386c

Browse files
committed
Replace SecCopyRandomBytes with Swift 4.2+ random
According to its [documentation](https://developer.apple.com/documentation/swift/systemrandomnumbergenerator) and of the functions underneath, this new default is also producing cryptographically secure random values. It also supports Linux.
1 parent 22f31a6 commit 9ed386c

File tree

1 file changed

+1
-10
lines changed

1 file changed

+1
-10
lines changed

Sources/Swift/Salt.swift

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66

77
import Foundation
8-
import Security
98

109
/// A class to wrap around Salts in argon2. allowing cryptographically secure generation of salts as well as utilization of premade salts
1110
public class Salt {
@@ -30,15 +29,7 @@ public class Salt {
3029
- Returns: A `Salt` object containing a random byte array of the specified length
3130
*/
3231
public static func newSalt(length: Int = 16) -> Salt {
33-
// Set a byte array
34-
var bytes = [UInt8](repeating: 0, count: length)
35-
// Set random generated numbers to the byte array
36-
let status = SecRandomCopyBytes(kSecRandomDefault, bytes.count, &bytes)
37-
// Ensure the copy was a success
38-
if status != errSecSuccess {
39-
fatalError("SecRandomCopyBytes failed with error code: \(status)")
40-
}
41-
// Return the salt
32+
let bytes = Array((0..<length).map { _ in UInt8.random(in: 0...255) })
4233
return Salt(bytes: Data(bytes))
4334
}
4435
}

0 commit comments

Comments
 (0)
0