Dav*_*idM 29 iphone cocoa-touch objective-c keychain
这个问题讨论使用crypt()函数在iPhone上加密数据.作为替代方案,iPhone上是否有钥匙串,如果有,我会使用什么代码来存储登录详细信息,然后在应用程序中为我们检索它们?
我非常喜欢Buzz Anderson的Keychain抽象层,我热切期待着Jens Alfke的MYCrypto达到一个可用的状态.后者可以使用相同的代码在Mac OS X和iPhone上使用,但其功能仅模仿Keychain的一小部分.
这是我用来存储钥匙串中的键/值对.确保将Security.framework添加到项目中
#import <Security/Security.h>
// -------------------------------------------------------------------------
-(NSString *)getSecureValueForKey:(NSString *)key {
/*
Return a value from the keychain
*/
// Retrieve a value from the keychain
NSDictionary *result;
NSArray *keys = [[[NSArray alloc] initWithObjects: (NSString *) kSecClass, kSecAttrAccount, kSecReturnAttributes, nil] autorelease];
NSArray *objects = [[[NSArray alloc] initWithObjects: (NSString *) kSecClassGenericPassword, key, kCFBooleanTrue, nil] autorelease];
NSDictionary *query = [[NSDictionary alloc] initWithObjects: objects forKeys: keys];
// Check if the value was found
OSStatus status = SecItemCopyMatching((CFDictionaryRef) query, (CFTypeRef *) &result);
[query release];
if (status != noErr) {
// Value not found
return nil;
} else {
// Value was found so return it
NSString *value = (NSString *) [result objectForKey: (NSString *) kSecAttrGeneric];
return value;
}
}
// -------------------------------------------------------------------------
-(bool)storeSecureValue:(NSString *)value forKey:(NSString *)key {
/*
Store a value in the keychain
*/
// Get the existing value for the key
NSString *existingValue = [self getSecureValueForKey:key];
// Check if a value already exists for this key
OSStatus status;
if (existingValue) {
// Value already exists, so update it
NSArray *keys = [[[NSArray alloc] initWithObjects: (NSString *) kSecClass, kSecAttrAccount, nil] autorelease];
NSArray *objects = [[[NSArray alloc] initWithObjects: (NSString *) kSecClassGenericPassword, key, nil] autorelease];
NSDictionary *query = [[[NSDictionary alloc] initWithObjects: objects forKeys: keys] autorelease];
status = SecItemUpdate((CFDictionaryRef) query, (CFDictionaryRef) [NSDictionary dictionaryWithObject:value forKey: (NSString *) kSecAttrGeneric]);
} else {
// Value does not exist, so add it
NSArray *keys = [[[NSArray alloc] initWithObjects: (NSString *) kSecClass, kSecAttrAccount, kSecAttrGeneric, nil] autorelease];
NSArray *objects = [[[NSArray alloc] initWithObjects: (NSString *) kSecClassGenericPassword, key, value, nil] autorelease];
NSDictionary *query = [[[NSDictionary alloc] initWithObjects: objects forKeys: keys] autorelease];
status = SecItemAdd((CFDictionaryRef) query, NULL);
}
// Check if the value was stored
if (status != noErr) {
// Value was not stored
return false;
} else {
// Value was stored
return true;
}
}
Run Code Online (Sandbox Code Playgroud)
值得注意的是,如果用户删除您的应用,则不会删除这些键/值.如果用户删除了您的应用,然后重新安装,则仍可以访问键/值.
归档时间: |
|
查看次数: |
52987 次 |
最近记录: |