[英]implementing PBKDF for encrypting a string in ios
我期待使用PBKDF2
實施加密。如果可能,請提供示例代碼。 關於實現PBKDF
加密字符串的問題..謝謝..
注意,這是一個示例,而不是生產代碼。
#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;
}
非常簡單的測試,使用CCCalibratePBKDF可以使用更好的鹽和更好的輪數。
- (void)test_doKeyForPassword {
NSData *key = [Crypto doKeyForPassword:@"password"
salt:[@"salt" dataUsingEncoding:NSUTF8StringEncoding]
keySize:kCCKeySizeAES128
rounds:1000];
NSLog(@"doKeyForPassword: %@",key);
}
如果要復制此代碼以在生產應用程序中使用:請勿。 這只是示例代碼。 基本上,如果需要此代碼,則不應進行加密。 雇用一位領域專家,至少要經過領域專家審核的代碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.