swi*_*ynx 5 random cryptography swift vapor
我想生成一个安全的随机数,用于 Vapor Swift 中的不记名令牌。
我看过OpenCrypto但它似乎无法生成随机数。
我该怎么做呢?
对于 Vapor,您可以像这样生成一个令牌:
[UInt8].random(count: 32).base64
Run Code Online (Sandbox Code Playgroud)
这将是加密安全的使用。您可以像在这个存储库中一样使用它
您可能想看看SecRandomCopyBytes(_:_:_:):
来自苹果文档:
生成加密安全随机字节数组。
var bytes = [Int8](repeating: 0, count: 10)
let status = SecRandomCopyBytes(kSecRandomDefault, bytes.count, &bytes)
if status == errSecSuccess { // Always test the status.
print(bytes)
// Prints something different every time you run.
}
Run Code Online (Sandbox Code Playgroud)