gpt4 book ai didi

ios - 从钥匙串(keychain)中检索 SecKey

转载 作者:可可西里 更新时间:2023-11-01 01:25:37 27 4
gpt4 key购买 nike

我正在尝试升级我从 this answer 获得的代码用于生成 CSR,从 Swift 2 到 Swift 3。

我已经升级了大部分代码,但原始答案的 Utility block 中的以下代码因错误而失败:

'init' is unavailable: use 'withMemoryRebound(to:capacity:_)' to temporarily view memory as another layout-compatible type.

错误发生在以下行:

let status: OSStatus = withUnsafeMutablePointer(to: &dataTypeRef) { SecItemCopyMatching(query as NSDictionary, UnsafeMutablePointer($0)) }
func loadKeySecKeyFromKeyChain(key: String) -> SecKey{
let query: Dictionary<String, AnyObject> = [
String(kSecAttrKeyType): kSecAttrKeyTypeRSA,
String(kSecAttrKeySizeInBits): KEY_SIZE as AnyObject,
String(kSecClass): kSecClassKey,
String(kSecAttrApplicationTag): key as AnyObject,
kSecReturnRef as String : kCFBooleanTrue ]

var dataTypeRef: Unmanaged<AnyObject>? = nil
var resultData: SecKey? = nil

let status: OSStatus = withUnsafeMutablePointer(to: &dataTypeRef) { SecItemCopyMatching(query as NSDictionary, UnsafeMutablePointer($0)) }
NSLog("SecItemCopyMatching: " + status.description)

if status == errSecSuccess {
NSLog("private or public debug description is: " + dataTypeRef.debugDescription)
resultData = (dataTypeRef!.takeRetainedValue() as! SecKey)
NSLog("SecItemCopyMatching returns SecKey: " + resultData.debugDescription)
return resultData!
} else {
return resultData!
}
}

我已经在这个问题上卡了一整天了,有什么解决这个错误的建议吗?

最佳答案

只需使用SecItemCopyMatching。我能够将它转换为 Swift 3 并成功生成 CSR。

// Finds the SecKeyRef corresponding to the parameter key and returns it
func loadKeySecKeyFromKeyChain(key: String) -> SecKey {
let query: Dictionary<String, AnyObject> = [
String(kSecAttrKeyType): kSecAttrKeyTypeRSA,
String(kSecAttrKeySizeInBits): KEY_SIZE as AnyObject,
String(kSecClass): kSecClassKey,
String(kSecAttrApplicationTag): key as AnyObject,
kSecReturnRef as String : kCFBooleanTrue ]

var dataTypeRef: Unmanaged<AnyObject>? = nil
var resultData: SecKey? = nil
var result: AnyObject?
let status = SecItemCopyMatching(query as CFDictionary, &result)

if status == errSecSuccess {
resultData = result as! SecKey
return resultData!
} else {
return resultData!
}
}

关于ios - 从钥匙串(keychain)中检索 SecKey,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41733166/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com