Retrieve SecKey from Keychain

前端 未结 1 525
执念已碎
执念已碎 2021-01-22 12:53

I am trying to upgrade the code that I got from this answer for generating CSR, from Swift 2 to Swift 3.

I have most of the code upgraded, but the following code in the

相关标签:
1条回答
  • 2021-01-22 13:01

    Just use SecItemCopyMatching. I was able to convert it to Swift 3 and successfully generate 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!
        }
    }
    
    0 讨论(0)
提交回复
热议问题