从钥匙串中删除私钥

时间:2018-06-07 18:12:48

标签: ios swift keychain

过去几天,我一直在努力尝试从钥匙串中删除生成的私钥。我一直在尽力按照Apple提供的文档来删除密钥,你可以找到here,但我还没有找到出错的地方。以下是我目前的代码:

func deleteKey() {
    var secret: AnyObject?
    // Retrieve Private Key
    let tag = "tag".data(using: .utf8)!
    print(secret)

    let getQuery: [String: Any] = [kSecClass as String: kSecClassKey, // This is a query we make to find the Private key in the key chain based of the input we put inside of it
        kSecAttrApplicationTag as String: tag, // We use the tag to search for our saved Private key in the KeyChain as it is unique
        kSecAttrKeyType as String: kSecAttrKeyTypeRSA, // This says the keychain will be of type RSA
        kSecReturnRef as String: kCFBooleanTrue,// This says to return a reference to the key, and not the key data itself
    ]

    let status = SecItemCopyMatching(getQuery as CFDictionary, &secret)
    print(secret!)

    let delQuery: [String: Any] = [
        kSecMatchItemList as String: secret,// Key to delete
    ]

    let delStatus = SecItemDelete(delQuery as CFDictionary)

    guard delStatus == errSecSuccess || delStatus == errSecItemNotFound else {
        print(delStatus)
        print("Error")
        return
    }

    print ("success")

我的delStatus变量一直在返回" -50",我知道这意味着我的搜索参数在查询中有问题,但老实说我无法搞清楚。在我等待答案的同时,我将继续努力解决这个问题,但任何帮助都将不胜感激。提前谢谢。

0 个答案:

没有答案