美文网首页
iOS KeyChain存储

iOS KeyChain存储

作者: A訫飛Flyme | 来源:发表于2017-11-08 13:09 被阅读44次

    KeyChain

    KeyChain 是苹果的密码管理系统,一个钥匙串可以包含多种类型的数据:密码 (-包括 网站,FTP服务器,SSH帐户,网络共享,无线网络,群组软件,加密磁盘映像 等-),私钥,电子证书 和加密笔记等
    主要讲的是开发用的东西,定义就不多赘述,有兴趣可以看上面wiki。

    常用
    关键代码
    //存
    [ToolUtils KeychainSave:PIN_KEYCHAIN data:newPwd];
    //取
    NSString *pin = [ToolUtils KeychainLoad:PIN_KEYCHAIN];
    //删
    [ToolUtils KeychainDelete:PIN_KEYCHAIN]
    
    #pragma mark -
    #pragma mark KeyChain methods
    + (NSMutableDictionary *)getKeychainQuery:(NSString *)service {
        return [NSMutableDictionary dictionaryWithObjectsAndKeys:
                (__bridge id)kSecClassGenericPassword,(__bridge id)kSecClass,
                service, (__bridge id)kSecAttrService,
                service, (__bridge id)kSecAttrAccount,
                (__bridge id)kSecAttrAccessibleAfterFirstUnlock,(__bridge id)kSecAttrAccessible,
                nil];
    }
    
    + (void)KeychainSave:(NSString *)service data:(id)data {
        //Get search dictionary
        NSMutableDictionary *keychainQuery = [self getKeychainQuery:service];
        //Delete old item before add new item
        SecItemDelete((__bridge CFDictionaryRef)keychainQuery);
        //Add new object to search dictionary(Attention:the data format)
        [keychainQuery setObject:[NSKeyedArchiver archivedDataWithRootObject:data] forKey:(__bridge id)kSecValueData];
        //Add item to keychain with the search dictionary
        SecItemAdd((__bridge CFDictionaryRef)keychainQuery, NULL);
    }
    
    + (id)KeychainLoad:(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:(__bridge id)kSecReturnData];
        [keychainQuery setObject:(__bridge id)kSecMatchLimitOne forKey:(__bridge id)kSecMatchLimit];
        CFDataRef keyData = NULL;
        if (SecItemCopyMatching((__bridge 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)KeychainDelete:(NSString *)service {
        NSMutableDictionary *keychainQuery = [self getKeychainQuery:service];
        SecItemDelete((__bridge CFDictionaryRef)keychainQuery);
    }
    

    相关文章

      网友评论

          本文标题:iOS KeyChain存储

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