美文网首页
基于钥匙串的保存

基于钥匙串的保存

作者: sydie | 来源:发表于2017-04-28 17:37 被阅读34次

封装了一些存储入钥匙串的方法。这个方法与本地存储的区别是什么呢?查看了大部分的文档解读,我觉得就是:
1.不易丢失。如果项目迭代更新、重装等等。存储的本地内容将不会存在。而钥匙串中的数据还能保存。
2.破解难度大。其他的本地存储截取更简单一些,所以用户名密码存储做的好点最好是不要放入本地存储中。加密放入钥匙串更加不易破解。


封装:
1.在Capabilities中勾选Keychain Sharing.
2.创建一个文件用于封装。
这里的封装适合保存一个值。多个值的封装需要传入保存的Key。
3.方法创建:

/**
 *  存储字符串到 KeyChain
 *
 *  @param string NSString
 */
+ (void)keyChainSave:(NSString *)string;
/**
 *  从 KeyChain 中读取存储的字符串
 *
 *  @return NSString
 */
+ (NSString *)keyChainLoad;
/**
 *  删除 KeyChain 信息
 */
+ (void)keyChainDelete;

4.方法实现

static NSString * const kPDDictionaryKey = @"com.xxx.dictionaryKey";
static NSString * const kPDKeyChainKey = @"com.xxx.keychainKey";
+ (void)keyChainSave:(NSString *)string {
    NSMutableDictionary *tempDic = [NSMutableDictionary dictionary];
    [tempDic setObject:string forKey:kPDDictionaryKey];
    [self save:kPDKeyChainKey data:tempDic];
}
+ (NSString *)keyChainLoad{
    NSMutableDictionary *tempDic = (NSMutableDictionary *)[self load:kPDKeyChainKey];
    return [tempDic objectForKey:kPDDictionaryKey];
}
+ (void)keyChainDelete{
    [self delete:kPDKeyChainKey];
}

内部方法


+ (void)save:(NSString *)service data:(id)data {
    //Get search dictionary
    NSMutableDictionary *keychainQuery = [self getKeychainQuery:service];
    //Delete old item before add new item
    SecItemDelete((CFDictionaryRef)keychainQuery);
    //Add new object to search dictionary(Attention:the data format)
    [keychainQuery setObject:[NSKeyedArchiver archivedDataWithRootObject:data] forKey:(id)kSecValueData];
    //Add item to keychain with the search dictionary
    SecItemAdd((CFDictionaryRef)keychainQuery, NULL);
}

+ (id)load:(NSString *)service {
    id ret = nil;
    NSMutableDictionary *keychainQuery = [self getKeychainQuery:service];
    //Configure the search setting
    //Since in our simple case we are expecting only a single attribute to be returned (the password) we can set the attribute kSecReturnData to kCFBooleanTrue
    [keychainQuery setObject:(id)kCFBooleanTrue forKey:(id)kSecReturnData];
    [keychainQuery setObject:(id)kSecMatchLimitOne forKey:(id)kSecMatchLimit];
    CFDataRef keyData = NULL;
    if (SecItemCopyMatching((CFDictionaryRef)keychainQuery, (CFTypeRef *)&keyData) == noErr) {
        @try {
            ret = [NSKeyedUnarchiver unarchiveObjectWithData:(__bridge NSData *)keyData];
        } @catch (NSException *e) {
            NSLog(@"Unarchive of %@ failed: %@", service, e);
        } @finally {
        }
    }
    if (keyData)
        CFRelease(keyData);
    return ret;
}

+ (void)delete:(NSString *)service {
    NSMutableDictionary *keychainQuery = [self getKeychainQuery:service];
    SecItemDelete((CFDictionaryRef)keychainQuery);
}

使用:

// 存
- (void)saveUseAccount {
        [PDKeyChain keyChainSave:self.userNameTextField.text];
}
// 读取
  // 从keychain里取账号信息
    self.userNameTextField.text = [PDKeyChain keyChainLoad];

ps:中间内容参考别人的,但是出处没有查到了。只是个人记录以后使用。

相关文章

  • 基于钥匙串的保存

    封装了一些存储入钥匙串的方法。这个方法与本地存储的区别是什么呢?查看了大部分的文档解读,我觉得就是:1.不易丢失。...

  • 7钥匙串加密

    钥匙串 第三方框架 sskeychain-master 保存到钥匙串 钥匙串访问的密码保存在哪里? 只有苹果知道,...

  • 数据安全之钥匙串加密

    简介:钥匙串(Keychain)iOS系统自带的一套敏感信息保存方案。保存钥匙串的内容相当于系统对其做了保护,在设...

  • 钥匙串保存用户信息

    引入头文件 只有点击删除,才可以将用户信息彻底删除干净,

  • 钥匙串加密(Keychain)方法

    1.钥匙串加密详解: 1.苹果iOS 和Mac OS X 系统自带了一套敏感信息保存方案:"钥匙串"(Keycha...

  • iOS开发之数据存储以及删除

    iOS本地数据保存有多种方式,归纳如下: |保存到KeyChain(钥匙串)|存储为文件|存储到数据库||:-:|...

  • iOS 钥匙串keychain的应用

    问题 目前需要保存一些用户配置,在app删掉后依然能正常读取,那么就用到【钥匙串】 钥匙串简介 项目中有时会需要存...

  • 推送证书无法导出成 p12

    1.检查证书是否从登录导出,系统钥匙串是不能导出 P12,如果证书保存在系统,那就把证书拷贝到登录钥匙串中再导出。...

  • git pull git push Access Token

    git pull 和git push时报错,现在GitHub都是用Access Token了。 先删除钥匙串里保存...

  • 通过钥匙串保存账号密码

    首先要找到ldandersen这个库,放到自己的工程里面去,由于这个库是不支持 arc 的,所以需要手动改一下,相...

网友评论

      本文标题:基于钥匙串的保存

      本文链接:https://www.haomeiwen.com/subject/ayxcgttx.html