Saving any object in Keychain

I want to save an object that may contain sensitive data in a keychain. To do this, I will convert this object to an instance of NSData.

I'm a little confused about which attributes I need to use. Is it possible to use kSecClassGenericPassword as kSecClass , although this is not a password? In addition, I installed kSecAttrAccount . I read somewhere that I also need to add kSecAttrService . What consequences can happen if I do not?

+4
source share
3 answers

Checkout SSKeychain on GitHub. This is a good open source shell for the keychain, which makes it very easy to use. If you want to learn more about how the keychain and security works in iOS / OS X, and you are registered with the Apple Developer Program, there are several WWDC session videos that you can check on developer.apple.com .

+2
source

Strongbox uses NSKeyedUnarchiver to convert any type in NSCoding to an NSData object before being stored in the keychain.

+1
source

See this library: https://github.com/nicklockwood/FXKeychain

- (BOOL)setObject:(id)object forKey:(id)key; - (BOOL)setObject:(id)object forKeyedSubscript:(id)key; 

Basic example:

 #import "ViewController.h" #import "FXKeychain.h" @interface ViewController () <UITextFieldDelegate, UITextViewDelegate> @property (nonatomic, strong) IBOutlet UITextField *keyField; @property (nonatomic, strong) IBOutlet UITextView *dataField; @end @implementation ViewController - (IBAction)save { //save data [FXKeychain defaultKeychain][_keyField.text] = _dataField.text; } - (IBAction)load { //load data _dataField.text = [FXKeychain defaultKeychain][_keyField.text]; } - (IBAction)delete { //clear field _dataField.text = @""; //delete data [[FXKeychain defaultKeychain] removeObjectForKey:_keyField.text]; } - (IBAction)tap { [_keyField resignFirstResponder]; [_dataField resignFirstResponder]; } - (BOOL)textFieldShouldReturn:(UITextField *)textField { [textField resignFirstResponder]; return YES; } @end 
0
source

Source: https://habr.com/ru/post/1486570/


All Articles