简体   繁体   English

使用 C/C++ 访问 OS X 钥匙串

[英]OS X Keychain access using C/C++

I am trying to write a simple test application to access OS X keychain.我正在尝试编写一个简单的测试应用程序来访问 OS X 钥匙串。 Add/Update/Delete an entry using C/C++.使用 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.我只是在测试我是否可以在我们拥有的更大的 C/C++ 代码库中使用它,我们需要安全的秘密存储,因此需要语言要求。

I looked up the API that Apple has on this but that is mostly in Objective-C.我查了苹果在这方面的 API,但主要在 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.我唯一能找到的是Apple 的安全工具,它看起来很旧,并且不确定 API 是否仍受支持。

Thanks in advance.提前致谢。

A minimal example showing how to add a password to the keychain using C:显示如何使用 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM