OS X Keychain access using C/C++

£可爱£侵袭症+ 提交于 2020-01-16 08:43:29

问题


I am trying to write a simple test application to access OS X keychain. Add/Update/Delete an entry using C/C++. I am just testing whether I can use this in a larger C/C++ code base that we have, where we need a secure secret storage, hence the language requirements.

I looked up the API that Apple has on this but that is mostly in Objective-C. Are there any solutions anyone is aware of? The only thing I could find was Apple's Security tool which seems old and am not sure if the APIs are still supported.

Thanks in advance.


回答1:


A minimal example showing how to add a password to the keychain using C:

#include <CoreFoundation/CoreFoundation.h>
#include <Security/Security.h>

int main(int argc, const char* argv[]) {
    CFStringRef keys[3];
    keys[0] = kSecClass;
    keys[1] = kSecAttrAccount;
    keys[2] = kSecValueData;

    CFTypeRef values[3];
    values[0] = kSecClassGenericPassword;
    values[1] = CFSTR("accountname");
    values[2] = CFSTR("password");

    CFDictionaryRef query;
    query = CFDictionaryCreate(kCFAllocatorDefault, (const void**) keys, (const void**) values, 3, NULL, NULL);

    OSStatus result = SecItemAdd(query, NULL);

    printf("%d", result);

    return 0;
}


来源:https://stackoverflow.com/questions/58828239/os-x-keychain-access-using-c-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!