AHKeychain 0.3.0

AHKeychain 0.3.0

测试已测试
Lang语言 Obj-CObjective C
许可 MIT
发布最近发布2016 年 9 月

Eldon Ahrold 维护。



  • 作者
  • Eldon Ahrold

用于管理 OSX 密钥链和密钥链项的 Objective-c 类。

本工程是从 SSKeychain https://github.com/soffes/sskeychain/ 衍生而来。

本项目有以下四个改进

  1. 它允许用户指定要使用哪个密钥链,例如系统密钥链或在外部驱动器/SD 卡上的密钥链。

  2. 另一个新增功能是可以指定一个被授予访问密钥链项的信任应用数组。

  3. 这还允许您更改密钥链的密码。

  4. 另一个小的改进是它实际上使用 SecItemUpdate() 更新密钥链项。SSKeychain 实际上是删除并重新添加密钥链项,这可能导致未签名代码的程序在适当地赋予权限时出现异常行为。

与特定密钥链一起工作。

指定默认登录密钥链
AHKeychain *keychain = [AHKeychain loginKeychain];
指定系统密钥链

要写入此密钥链,您必须以 root 权限运行您的应用程序

AHKeychain *keychain = [AHKeychain systemKeychain];
指定特定路径(外部驱动器)的密钥链
AHKeychain *keychain = [AHKeychain keychainAtPath:@"/Volumes/MyExternalHD/Library/Keychains/myextkc.keychain"];
创建新的用户密钥链
AHKeychain *keychain = [AHKeychain alloc]initCreatingNewKeychain:@"Test Keychain"
                                                    password:@"realfakepsswd"];
删除密钥链文件。这将破坏性!

*在登录密钥链或系统密钥链上调用此方法将失败

[keychain deleteKeychain];

修改密钥链项。

添加/更新项
AHKeychainItem *item = [AHkeychainItem alloc] init];
item.service = @"com.eeaapps.test";
item.account = @"myusername";
item.label = @"AHKeychain Test Keychain Item";
item.password = @"mysecretpass";

// also if you want to allow other app to access the keychain item
NSArray *trustedApps = [NSArray arrayWithObjects:@"/Applications/Mail.app",
                                                 @"/Applications/Preview.app",
                                                 nil];
item.trustedApplications = trustedApps;

[keychain saveItem:item error:&error];
获取项密码
AHKeychainItem *item = [AHkeychainItem alloc] init];
item.service = @"com.eeaapps.test";
item.account = @"myusername";

[keychain getItem:item error:&error];

NSLog(@"The Password is %@",item.password);
移除密钥链项
AHKeychainItem *item = [AHkeychainItem alloc] init];
item.service = @"com.eeaapps.test";
item.account = @"myusername";
[keychain deleteItem:item error:&error];

便捷的类方法

存在两个针对标准钥匙串的常量

kAHKeychainLoginKeychain
kAHKeychainSystemKeychain

您可以使用这些中的一个,或在以下方法中指定钥匙串文件的完整路径

设置密码
[AHKeychain setPassword:@"mysecretpass" 
                service:@"com.eeaapps.testkc" 
                account:@"myusername" 
               keychain:kAHKeychainLoginKeychain 
                  error:&error];
获取密码
NSError *error;    
NSString *password = [AHKeychain getPasswordForService:item.service
                                               account:item.account
                                              keychain:kAHKeychainLoginKeychain
                                                 error:&error];

NSLog(@"The Password is %@",item.password);

更多信息请参阅AHKeychain.h和AHKeychainItem.h。