NCFUSN
NCFUSN

Reputation: 1644

iOS, Keychain: Wrong passcode output

OK. I set user passcode in the view controller:

KeychainItemWrapper *keychainWrapper =
[[KeychainItemWrapper alloc] initWithIdentifier:
 [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleIdentifier"] accessGroup:nil];

[keychainWrapper setObject:@"theUser" forKey:(__bridge id)(kSecAttrAccount)];
[keychainWrapper setObject:_passcode forKey:(__bridge id)(kSecValueData)];
NSLog(@"Saved pass: %@",_passcode);
NSLog(@"Returned pass: %@",[keychainWrapper objectForKey:(__bridge id)(kSecValueData)]);

NSlogs return the same string I set, lets assume it's 1234. Later in AppDelegate I try to retrieve the passcode:

KeychainItemWrapper *keychainWrapper =
[[KeychainItemWrapper alloc] initWithIdentifier:
 [[[NSBundle mainBundle]infoDictionary] objectForKey:@"CFBundleIdentifier"] accessGroup:nil];
passcodeViewController.passcode = [keychainWrapper objectForKey:(__bridge id)(kSecValueData)];
NSLog(@"Pass: %@",[keychainWrapper setObject:_passcode forKey:(__bridge id)(kSecValueData)]);

And then the evil comes. NSLog returns: 31323334. What wrong I am doing?

Upvotes: 0

Views: 381

Answers (1)

Fruity Geek
Fruity Geek

Reputation: 7381

Keychain stores the password as NSData, not NSString. Before you store the password, you need to encode it.

NSData * passcodeData = [_passcode dataUsingEncoding:NSUTF8StringEncoding];
[keychainWrapper setObject:passcodeData forKey:(__bridge id)(kSecValueData)];

And similarly, the object retrieved is NSData which you need to decode.

NSData * passcodeData = [keychainWrapper objectForKey:(__bridge id)(kSecValueData)];
passcodeViewController.passcode = [[NSString alloc] initWithBytes:[passcodeData bytes]                                                             
                                                           length:[passcodeData length] 
                                                         encoding:NSUTF8StringEncoding];

Upvotes: 2

Related Questions