As requested: Sample code on implementing PBKDF
Note, this is an example, not production code.
#import <CommonCrypto/CommonKeyDerivation.h>
+ (NSData *)doKeyForPassword:(NSString *)password
salt:(NSData *)salt
keySize:(NSUInteger)keySize
rounds:(NSUInteger)rounds {
NSMutableData *derivedKey = [NSMutableData dataWithLength:keySize];
NSData *passwordData = [password dataUsingEncoding: NSUTF8StringEncoding];
CCKeyDerivationPBKDF(kCCPBKDF2, // algorithm
passwordData, // password
passwordData, // passwordLength
salt.bytes, // salt
salt.length, // saltLen
kCCPRFHmacAlgSHA1, // PRF
rounds, // rounds
derivedKey.mutableBytes, // derivedKey
derivedKey.length); // derivedKeyLen
return derivedKey;
}
Very simple test, use a better salt
and a better rounds count, possible using CCCalibratePBKDF.
- (void)test_doKeyForPassword {
NSData *key = [Crypto doKeyForPassword:@"password"
salt:[@"salt" dataUsingEncoding:NSUTF8StringEncoding]
keySize:kCCKeySizeAES128
rounds:1000];
NSLog(@"doKeyForPassword: %@",key);
}
If you are copying this code to use in a production app: Don't. This is just example code. Basically if one needs this code they should not be doing cryptography. Hire a domain expert, at a minimum have the code vetted by a domain expert.