使用Swift在iOS中创建安全随机数? public func createSecureRandomKey(numberOfBits:Int)->任意{ let属性:[字符串:任意]= [kSecAttrKeyType作为字符串:CFString.self, kSecAttrKeySizeInBits作为字符串:numberOfBits] var错误:非托管? guard let privateKey=SecKeyCreateRandomKey(属性为CFDictionary,&错误)else{ 返回“” } 返回私钥 }
我试图创建安全的随机数像上面的方式,但什么也不返回,请任何人帮我。谢谢。看起来您使用了错误的功能。使用函数生成一个新密钥。但正如你的标题所说,你想要生成安全的随机数。 为此,有一个函数名为:SecRandomCopyBytes(::。) 以下是一段代码片段,摘自苹果官方文档《如何使用它》:使用Swift在iOS中创建安全随机数? public func createSecureRandomKey(numberOfBits:Int)->任意{ let属性:[字符串:任意]= [kSecAttrKeyType作为字符串:CFString.self, kSecAttrKeySizeInBits作为字符串:numberOfBits] var错误:非托管? guard let privateKey=SecKeyCreateRandomKey(属性为CFDictionary,&错误)else{ 返回“” } 返回私钥 },swift,cryptography,swift4,Swift,Cryptography,Swift4,我试图创建安全的随机数像上面的方式,但什么也不返回,请任何人帮我。谢谢。看起来您使用了错误的功能。使用函数生成一个新密钥。但正如你的标题所说,你想要生成安全的随机数。 为此,有一个函数名为:SecRandomCopyBytes(::。) 以下是一段代码片段,摘自苹果官方文档《如何使用它》: public func createSecureRandomKey(numberOfBits: Int) -> Any { let attributes: [String: Any] =
public func createSecureRandomKey(numberOfBits: Int) -> Any {
let attributes: [String: Any] =
[kSecAttrKeyType as String:CFString.self,
kSecAttrKeySizeInBits as String:numberOfBits]
var error: Unmanaged<CFError>?
guard let privateKey = SecKeyCreateRandomKey(attributes as CFDictionary, &error) else {
return ""
}
return privateKey
}
来源:您是否尝试打印
错误
?是。。但是没有显示错误,总是在防护装置内返回“添加打印(错误)
”。
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.
}