本文介绍了在iOS上使用CommonCrypto的PBKDF2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用 CommonCrypto
使用 PBKDF2
生成密钥,但我似乎无法导入 CommonCrypto / CommonKeyDerivation.h
,我只是找不到它的错误。
I'm trying to use CommonCrypto
to generate keys using PBKDF2
but I can't seem to import CommonCrypto/CommonKeyDerivation.h
, I just errors that it is not found.
有什么想法吗?
编辑:我应该提一下我已经添加了安全框架,我可以导入所有其他 CommonCrypto
标题。
edit: I should probably mention I have already added the security framework and I can import all of the other CommonCrypto
headers.
推荐答案
以下是我生成AES256密钥的方法。唯一有趣的是,我得到CommonCrypto来估计我要使用多少轮。这似乎非常简单。
Here's how i generate AES256 keys. The only interesting this is that i get CommonCrypto to estimate for me how many rounds to use. It seems pretty straightforwards.
#import <CommonCrypto/CommonKeyDerivation.h>
...
// Makes a random 256-bit salt
- (NSData*)generateSalt256 {
unsigned char salt[32];
for (int i=0; i<32; i++) {
salt[i] = (unsigned char)arc4random();
}
return [NSData dataWithBytes:salt length:32];
}
...
// Make keys!
NSString* myPass = @"MyPassword1234";
NSData* myPassData = [myPass dataUsingEncoding:NSUTF8StringEncoding];
NSData* salt = [self generateSalt256];
// How many rounds to use so that it takes 0.1s ?
int rounds = CCCalibratePBKDF(kCCPBKDF2, myPassData.length, salt.length, kCCPRFHmacAlgSHA256, 32, 100);
// Open CommonKeyDerivation.h for help
unsigned char key[32];
CCKeyDerivationPBKDF(kCCPBKDF2, myPassData.bytes, myPassData.length, salt.bytes, salt.length, kCCPRFHmacAlgSHA256, rounds, key, 32);
这篇关于在iOS上使用CommonCrypto的PBKDF2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!