我正在尝试获取钥匙链项的属性。这段代码应该查找所有可用的属性,然后打印出它们的标记和内容。
根据the docs的说法,我应该看到像'cdat‘这样的标签,但它们看起来只是一个索引(即,第一个标签是0,下一个是1)。这使得它变得非常无用,因为我不知道哪个属性是我要寻找的属性。
SecItemClass itemClass;
SecKeychainItemCopyAttributesAndData(itemRef, NULL, &itemClass, NULL, NULL, NULL);
SecKeychainRef keychainRef;
SecKeychainItemCopyKeychain(itemRef, &keychainRef);
SecKeychainAttributeInfo *attrInfo;
SecKeychainAttributeInfoForItemID(keychainRef, itemClass, &attrInfo);
SecKeychainAttributeList *attributes;
SecKeychainItemCopyAttributesAndData(itemRef, attrInfo, NULL, &attributes, 0, NULL);
for (int i = 0; i < attributes->count; i ++)
{
SecKeychainAttribute attr = attributes->attr[i];
NSLog(@"%08x %@", attr.tag, [NSData dataWithBytes:attr.data length:attr.length]);
}
SecKeychainFreeAttributeInfo(attrInfo);
SecKeychainItemFreeAttributesAndData(attributes, NULL);
CFRelease(itemRef);
CFRelease(keychainRef);发布于 2010-03-26 09:37:41
这里有两件事你应该做。首先,您需要在调用SecKeychainAttributeInfoForItemID之前处理“通用”itemClasses ...
switch (itemClass)
{
case kSecInternetPasswordItemClass:
itemClass = CSSM_DL_DB_RECORD_INTERNET_PASSWORD;
break;
case kSecGenericPasswordItemClass:
itemClass = CSSM_DL_DB_RECORD_GENERIC_PASSWORD;
break;
case kSecAppleSharePasswordItemClass:
itemClass = CSSM_DL_DB_RECORD_APPLESHARE_PASSWORD;
break;
default:
// No action required
}其次,您需要将attr.tag从FourCharCode转换为字符串,即
NSLog(@"%c%c%c%c %@",
((char *)&attr.tag)[3],
((char *)&attr.tag)[2],
((char *)&attr.tag)[1],
((char *)&attr.tag)[0],
[[[NSString alloc]
initWithData:[NSData dataWithBytes:attr.data length:attr.length]
encoding:NSUTF8StringEncoding]
autorelease]]);注意,我还将数据输出为字符串--它几乎总是UTF8编码的数据。
发布于 2009-07-27 04:57:34
我认为文档会导致一些混乱。
我看到的数字似乎是keychain item attribute constants for keys。
但是,SecKeychainItemCopyAttributesAndData返回一个SecKeychainAttributeList结构,该结构包含一个SecKeychainAttributes数组。来自TFD:
标签一个4字节的属性标签。有关有效的属性类型,请参阅“Keychain Item Attribute Constants”。
属性常量(非“for key”类型)是我期望看到的4个字符的值。
https://stackoverflow.com/questions/1186385
复制相似问题