Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Swift在iOS中创建安全随机数? public func createSecureRandomKey(numberOfBits:Int)->任意{ let属性:[字符串:任意]= [kSecAttrKeyType作为字符串:CFString.self, kSecAttrKeySizeInBits作为字符串:numberOfBits] var错误:非托管? guard let privateKey=SecKeyCreateRandomKey(属性为CFDictionary,&错误)else{ 返回“” } 返回私钥 }_Swift_Cryptography_Swift4 - Fatal编程技术网

使用Swift在iOS中创建安全随机数? public func createSecureRandomKey(numberOfBits:Int)->任意{ let属性:[字符串:任意]= [kSecAttrKeyType作为字符串:CFString.self, kSecAttrKeySizeInBits作为字符串:numberOfBits] var错误:非托管? guard let privateKey=SecKeyCreateRandomKey(属性为CFDictionary,&错误)else{ 返回“” } 返回私钥 }

使用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] =

我试图创建安全的随机数像上面的方式,但什么也不返回,请任何人帮我。谢谢。

看起来您使用了错误的功能。使用函数生成一个新密钥。但正如你的标题所说,你想要生成安全的随机数。 为此,有一个函数名为:SecRandomCopyBytes(::。)

以下是一段代码片段,摘自苹果官方文档《如何使用它》:

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.
}