前言 :
項目中有時會需要存儲敏感信息(如密碼、密鑰等),蘋果官方提供了一種存儲機制--鑰匙串(keychain)
。
keychain是一種存儲在硬盤
上的加密的數據庫
。這個可能是卸載App后,keychain信息還在的原因。
keychain適合存儲較小的數據量
(不超過上千字節或上兆字節
)的內容。
筆者做了一個關于keychain的增、刪、改、查
的Demo(QiKeychain),給大家介紹下keychain的基本使用。
下圖(確保keychain中用戶的信息安全)有利于我們直觀了解keychain。
keychain存儲密碼密鑰證書標識日志等
Demo(QiKeychain)解讀:
筆者用Demo(QiKeychain)做了4件事。
- 增加:存儲用戶名、密碼到keychain;
- 查詢:根據用戶名從keychain中查詢密碼;
- 刪除:從keychain中刪除用戶名、密碼等相應信息;
- 修改:修改keychain中的用戶名對應的密碼;
Demo(QiKeychain)對keychain的操作效果如下:
- 存儲用戶名 “QiShare”,密碼:1234;
- 查詢用戶名為“QiShare”的密碼,顯示密碼為:1234;
- 修改用戶名“QiShare”的密碼為“123456”;
- 查詢“QiShare”的密碼,顯示為“123456”;
- 把“QiShare”從keychain中刪除。
keychain基本使用API
keychain有四個常用的API,用于增、刪、改、查keychain中的數據。
keychain中的數據子項是以item的形式存在的。
舉個例子:就存儲用戶名、密碼的情景來說,每個item包含存儲的用戶名和密碼及其他屬性信息,keychain中包含多個用戶名和密碼的item。
下圖(把數據和屬性存儲到keychain中)利于我們理解存儲過程
把數據和屬性存儲到keychain中
SecItemAdd:添加一個item或多個items到keychain
OSStatus SecItemAdd(CFDictionaryRef attributes, CFTypeRef * __nullable CF_RETURNS_RETAINED result)
API_AVAILABLE(macos(10.6), ios(2.0));
存儲關鍵代碼:
NSDictionary *saveSecItems = @{(id)kSecClass: (id)kSecClassGenericPassword,
(id)kSecAttrService: service,
(id)kSecAttrAccount: account,
(id)kSecValueData: passwordData
};
OSStatus saveStatus = SecItemAdd((CFDictionaryRef)saveSecItems, NULL);
SecItemCopyMatching:返回匹配搜索查詢的一個item或多個items
OSStatus SecItemCopyMatching(CFDictionaryRef query, CFTypeRef * __nullable CF_RETURNS_RETAINED result)
API_AVAILABLE(macos(10.6), ios(2.0));
查詢關鍵代碼:
NSDictionary *matchSecItems = @{
(id)kSecClass: (id)kSecClassGenericPassword,
(id)kSecAttrService: service,
(id)kSecAttrAccount: account,
(id)kSecMatchLimit: (id)kSecMatchLimitOne,
(id)kSecReturnData: @(YES)
};
CFTypeRef dataRef = nil;
OSStatus errorCode = SecItemCopyMatching((CFDictionaryRef)matchSecItems, (CFTypeRef *)&dataRef);
SecItemUpdate:修改匹配搜索查詢的一個item或多個items
OSStatus SecItemUpdate(CFDictionaryRef query, CFDictionaryRef attributesToUpdate)
API_AVAILABLE(macos(10.6), ios(2.0));
注意:更新代碼這部分,筆者開始的時候遇到一些問題,還要多謝組內成員,尤其是昆哥的指教。
SecItemUpdate接收了2個參數,query和attributesToUpdate。
第一個參數query用于查詢到相應的item,
第二個參數attributesToUpdate用于傳入要更新的信息。
筆者曾錯誤地給第二個參數attributesToUpdate傳入過(id)kSecClass: (id)kSecClassGenericPassword要更改的內容。
結果報錯為:
errSecNoSuchAttr = -25303, /* The specified attribute does not exist. */
更新關鍵代碼:
NSDictionary *queryItems = @{(id)kSecClass: (id)kSecClassGenericPassword,
(id)kSecAttrService: service,
(id)kSecAttrAccount: account
};
NSData *passwordData = [password dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *updatedItems = @{
(id)kSecValueData: passwordData,
};
OSStatus updateStatus = SecItemUpdate((CFDictionaryRef)queryItems, (CFDictionaryRef)updatedItems);
SecItemDelete:刪除匹配搜索查詢的一個item或多個items
OSStatus SecItemDelete(CFDictionaryRef query)
API_AVAILABLE(macos(10.6), ios(2.0));
刪除關鍵代碼:
NSDictionary *deleteSecItems = @{
(id)kSecClass: (id)kSecClassGenericPassword,
(id)kSecAttrService: service,
(id)kSecAttrAccount: account
};
OSStatus errorCode = SecItemDelete((CFDictionaryRef)deleteSecItems);
-
顯然keychain的
增刪改查
相關的API都需要設置相應的屬性字典
(分別代指上述的saveSecItems 、matchSecItems 、queryItems 、updatedItems 、deleteSecItems) - 屬性字典的key、value常用的有:(這部分內容讀者也可直接看文檔)
- (id)kSecClass: (id)kSecClassGenericPassword
kSecClass表示item的class
(id)kSecClass的值表明一個通用的密碼item筆者一般都傳入kSecClassGenericPassword- (id)kSecAttrService: service
kSecAttrService的value用于表明item的service- (id)kSecAttrAccount: account
(id)kSecAttrAccoun的值表明item的帳戶名- (id)kSecValueData: passwordData
(id)kSecValueData表示item的數據- (id)kSecMatchLimit: (id)kSecMatchLimitOne,
(id)kSecMatchLimit 有2個值(id)kSecMatchLimitOne、和(id)kSecMatchLimitAll
kSecMatchLimitOne:表示只匹配第一個符合條件的item
kSecMatchLimitAll:表示匹配不限數量的items- (id)kSecReturnData: @(YES)
(id)kSecReturnData的值是一個Boolean類型的值用于確定是否返回item data- kSecClass的值表示item的class
kSecClass的值表明一個通用的密碼item筆者一般都傳入的kSecClassGenericPassword- kSecClass的值表示item的class
kSecClass的值表明一個通用的密碼item筆者一般都傳入的kSecClassGenericPassword
Demo(QiKeychain)相關代碼
在Demo(QiKeychain)中,筆者對keychain相關使用的API進行了封裝。獲取Demo(QiKeychain)GitHub地址:QiKeychain。
注意:
筆者后來封裝的代碼,修改了保存操作的邏輯。
修改內容為:在保存用戶名密碼的時候
-> 先在keychain中查詢
用戶名是否存在
-> 若存在,就進行更新
操作;
-> 若不存在就進行保存
操作。
相應示意圖(使用鑰匙串存儲網絡密碼)如下:
使用鑰匙串存儲網絡密碼
參考學習地址
推薦文章:
iOS 自定義拖拽式控件:QiDragView
iOS 自定義卡片式控件:QiCardView
iOS Wireshark抓包
iOS Charles抓包
初探TCP
IP、UDP初探