gpt4 book ai didi

macos - 在 Mac OS X 上列出钥匙串(keychain)中的条目

转载 作者:行者123 更新时间:2023-12-04 23:07:25 24 4
gpt4 key购买 nike

过去几天我一直在玩 Cocoa,我想知道如何列出我创建的钥匙串(keychain)的所有名称/帐户对? Mac OS X 附带的小 key 链访问应用程序可以做到这一点,所以我想一定有可能吗? SecItemCopyMatching 是我要找的吗?但是,如何指定要搜索的钥匙串(keychain)?在这种情况下,服务名称是什么?

...我是唯一一个认为 Cocoa 中的 Keychain API 绝对可怕的人吗?在过去的几个小时左右,我一直在上下阅读文档,但我仍然无处可去:-/

最佳答案

您使用 SecItemCopyMatching 遍历钥匙串(keychain)中的项目并使用 SecKeychainFindInternetPassword 访问密码或 SecKeychainFindGenericPassword .

遍历钥匙串(keychain) :

// iterates over keychain and pass every item found by the query to PrintAccount.
static void IterateOverKeychain() {
// create query
CFMutableDictionaryRef query = CFDictionaryCreateMutable(kCFAllocatorDefault, 3, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
CFDictionaryAddValue(query, kSecReturnAttributes, kCFBooleanTrue);
CFDictionaryAddValue(query, kSecMatchLimit, kSecMatchLimitAll);
CFDictionaryAddValue(query, kSecClass, kSecClassInternetPassword);

// get search results
CFArrayRef result = nil;
OSStatus status = SecItemCopyMatching(query, (CFTypeRef*)&result);
assert(status == 0);

// do something with the result
CFRange range = CFRangeMake(0, CFArrayGetCount(result));
CFArrayApplyFunction(result, range, PrintAccount, nil);
}

// prints the password for a item from the keychain.
static void PrintAccount(const void *value, void *context) {
CFDictionaryRef dict = value;
CFStringRef acct = CFDictionaryGetValue(dict, kSecAttrAccount);
NSLog(@"%@", acct);
}

打印密码 :
static void PrintPassword() {
const char *acct = "foo.bar@googlemail.com";
UInt32 acctLen = (UInt32)strlen(acct);

const char *srvr = "calendar.google.com";
UInt32 srvrLen = (UInt32)strlen(srvr);

UInt32 pwLen = 0;
void *pw = 0;

SecKeychainFindInternetPassword(nil, srvrLen, srvr, 0, nil, acctLen, acct, 0, nil, 0, kSecProtocolTypeAny, kSecAuthenticationTypeAny, &pwLen, &pw, nil);

CFStringRef pwString = CFStringCreateWithBytes(kCFAllocatorDefault, pw, pwLen, kCFStringEncodingUTF8, NO);
NSLog(@"%s %@", acct, pwString);
}

关于macos - 在 Mac OS X 上列出钥匙串(keychain)中的条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8645652/

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