gpt4 book ai didi

objective-c - 如何在 OS X 中成功删除钥匙串(keychain)中的项目?

转载 作者:行者123 更新时间:2023-11-30 17:34:19 28 4
gpt4 key购买 nike

如何在 OS X 中成功删除钥匙串(keychain)中的项目?这是我到目前为止所拥有的,但它在 SecItemDelete 调用时失败。我需要在查询字典中指定一些额外的属性吗?我可以通过执行类似的操作成功地将键值对插入到钥匙串(keychain)中。

#define DELETE_SIZE 3
bool delete_key_keychain( const char *cKeyValue, SecKeychainRef keychain) {
if ( !keychain)
return false;

// Convert to CFString.
CFStringRef keyValue = CFStringCreateWithCString( NULL, cKeyValue, kCFStringEncodingUTF8);
if ( !keyValue)
return false;

// Specify query parameters.
const void *keys[DELETE_SIZE] = {
kSecClass,
kSecUseKeychain,
kSecAttrAccount
};
const void *values[DELETE_SIZE] = {
kSecClassGenericPassword,
keychain,
keyValue
};

// Create query.
CFDictionaryRef query = CFDictionaryCreate( NULL, keys, values, GET_SIZE, NULL, NULL);
if ( !query) {
CFRelease( keyValue);
return false;
}

// Run query.
OSStatus status = SecItemDelete( query);
CFRelease( query);
CFRelease( keyValue);

return status == errSecSuccess;
}

更新:根据此link ,我需要在查询中设置 kSecAttrService 属性。因此,我开始使用 SecKeychainFindGenericPassword 和 SecKeychainItemDelete。这将成功删除密码,但是随后为同一帐户添加不同的密码可能会导致崩溃。因此,我开始使用 SecKeychainItemModifyAttributesAndData 来修改密码并且不删除密码。我认为这种不稳定可能是由于 API 执行的缓存造成的。

最佳答案

过去,我曾使用此功能从我的 Mac OS 钥匙串(keychain)中删除特定的公钥/私钥对项目

// remove keypair from keychain
- (BOOL)deleteKeysFromKeychain {
OSStatus sanityCheck1 = noErr;
OSStatus sanityCheck2 = noErr;

NSMutableDictionary * queryPublicKey = [[NSMutableDictionary alloc] init];
NSMutableDictionary * queryPrivateKey = [[NSMutableDictionary alloc] init];

// Set the public key query dictionary.
[queryPublicKey setObject:(__bridge id)kSecClassKey forKey:(__bridge id)kSecClass];
[queryPublicKey setObject:self.myPublicTag forKey:(__bridge id)kSecAttrApplicationTag];
[queryPublicKey setObject:(__bridge id)kSecAttrKeyTypeRSA forKey:(__bridge id)kSecAttrKeyType];

// Set the private key query dictionary.
[queryPrivateKey setObject:(__bridge id)kSecClassKey forKey:(__bridge id)kSecClass];
[queryPrivateKey setObject:self.myPrivateTag forKey:(__bridge id)kSecAttrApplicationTag];
[queryPrivateKey setObject:(__bridge id)kSecAttrKeyTypeRSA forKey:(__bridge id)kSecAttrKeyType];

// Delete the private key.
sanityCheck1 = SecItemDelete((__bridge CFDictionaryRef)queryPrivateKey);

// Delete the public key.
sanityCheck2 = SecItemDelete((__bridge CFDictionaryRef)queryPublicKey);

@autoreleasepool {
queryPrivateKey = nil;
queryPublicKey = nil;
}

return ( sanityCheck1 == sanityCheck2 && sanityCheck2 == noErr ? YES : NO );
}

其中 myPublicTag 类似于 'com.yourAppName.publickey'

其中 myPrivateTag 类似于 'com.yourAppName.privatekey'

也许这会有用

关于objective-c - 如何在 OS X 中成功删除钥匙串(keychain)中的项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23415461/

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