使用C/C ++的OS X钥匙串访问 [英] OS X Keychain access using C/C++

查看:81
本文介绍了使用C/C ++的OS X钥匙串访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个简单的测试应用程序来访问OS X钥匙串.使用C/C ++添加/更新/删除条目.我只是在测试是否可以在我们拥有的更大的C/C ++代码库中使用此代码,在该代码库中我们需要安全的秘密存储,因此需要语言.

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.

我查找了Apple拥有的API,但这主要是在Objective-C中.有没有人知道的解决方案?我唯一能找到的是 Apple的安全工具,它看起来既旧又旧不确定是否仍支持这些API.

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.

先谢谢了.

推荐答案

一个最小的示例,展示了如何使用C:

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;
}

这篇关于使用C/C ++的OS X钥匙串访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆