无法成功调用 SecItemAdd
Unable to call SecItemAdd Successfully
我正在尝试通过 C++ API 向 macOS 中的钥匙串添加一个简单的字符串秘密。不幸的是,我打给 SecItemAdd
的电话打不通。我知道这是因为 kSecValueRef
键的值是错误的类型,但到目前为止 Google/Apple docs/ 还没有向我透露我应该使用什么类型,以及如何创造它。这是我目前所拥有的:
CFStringRef keys[4];
keys[0] = kSecClass;
keys[1] = kSecValueRef;
keys[2] = kSecAttrAccount;
keys[3] = kSecAttrService;
CFTypeRef values[4];
values[0] = kSecClassGenericPassword;
values[1] = CFSTR("password");
values[2] = CFSTR( "account-1" );
values[3] = CFSTR( "service-1" );
CFDictionaryRef attributes = CFDictionaryCreate
(
( CFAllocatorRef )NULL,
( const void ** )keys,
( const void ** )values,
4,
&kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks
);
CFShow(attributes);
OSStatus status = SecItemAdd(attributes, NULL);
那么我应该为 kSecValueRef
添加什么?我试过引用 CFStringRef,但这没有用。我还在 Apple docs 中看到它说:
The corresponding value, depending on the item class requested, is of type SecKeychainItem, SecKey, SecCertificate, or SecIdentity.
但是我没有 SecKeychainItem
因为我还没有添加项目,而且其他类型似乎不适合普通字符串。
想法?
想通了!对于字符串等简单数据,您应该使用 kSecValueData
键。那会让你只传递一个 CFStringRef
.
示例:
CFStringRef keys[4];
keys[0] = kSecClass;
keys[1] = kSecAttrAccount;
keys[2] = kSecAttrService;
keys[3] = kSecValueData;
CFTypeRef values[4];
values[0] = kSecClassGenericPassword;
values[1] = CFSTR( "account-15" );
values[2] = CFSTR( "service-126" );
values[3] = CFSTR( "password" );
我正在尝试通过 C++ API 向 macOS 中的钥匙串添加一个简单的字符串秘密。不幸的是,我打给 SecItemAdd
的电话打不通。我知道这是因为 kSecValueRef
键的值是错误的类型,但到目前为止 Google/Apple docs/
CFStringRef keys[4];
keys[0] = kSecClass;
keys[1] = kSecValueRef;
keys[2] = kSecAttrAccount;
keys[3] = kSecAttrService;
CFTypeRef values[4];
values[0] = kSecClassGenericPassword;
values[1] = CFSTR("password");
values[2] = CFSTR( "account-1" );
values[3] = CFSTR( "service-1" );
CFDictionaryRef attributes = CFDictionaryCreate
(
( CFAllocatorRef )NULL,
( const void ** )keys,
( const void ** )values,
4,
&kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks
);
CFShow(attributes);
OSStatus status = SecItemAdd(attributes, NULL);
那么我应该为 kSecValueRef
添加什么?我试过引用 CFStringRef,但这没有用。我还在 Apple docs 中看到它说:
The corresponding value, depending on the item class requested, is of type SecKeychainItem, SecKey, SecCertificate, or SecIdentity.
但是我没有 SecKeychainItem
因为我还没有添加项目,而且其他类型似乎不适合普通字符串。
想法?
想通了!对于字符串等简单数据,您应该使用 kSecValueData
键。那会让你只传递一个 CFStringRef
.
示例:
CFStringRef keys[4];
keys[0] = kSecClass;
keys[1] = kSecAttrAccount;
keys[2] = kSecAttrService;
keys[3] = kSecValueData;
CFTypeRef values[4];
values[0] = kSecClassGenericPassword;
values[1] = CFSTR( "account-15" );
values[2] = CFSTR( "service-126" );
values[3] = CFSTR( "password" );