首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >PBKDF2在iOS上使用CommonCrypto

PBKDF2在iOS上使用CommonCrypto
EN

Stack Overflow用户
提问于 2011-12-20 08:53:52
回答 3查看 16.8K关注 0票数 16

我试图通过PBKDF2使用CommonCrypto来生成密钥,但是我似乎不能导入CommonCrypto/CommonKeyDerivation.h,我只是错误地说找不到它。

有什么想法吗?

编辑:我应该提一下,我已经添加了安全框架,并且我可以导入所有其他CommonCrypto头文件。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-02-21 12:49:13

下面是生成AES256密钥的方法。唯一有趣的是,我让CommonCrypto为我估计使用了多少轮。这看起来很简单。

代码语言:javascript
运行
复制
#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);
票数 32
EN

Stack Overflow用户

发布于 2013-09-03 17:56:52

  1. 将此库添加到项目libcommonCrypto.dylib
  2. #import中的哈希键生成类中。
  3. 使用以下代码生成哈希键。

这是我使用的代码:

代码语言:javascript
运行
复制
// Salt data getting from salt string.
NSData *saltData = [@"Salt String" dataUsingEncoding:NSUTF8StringEncoding];

// Data of String to generate Hash key(hexa decimal string).
NSData *passwordData = [@"Hash key generated string" dataUsingEncoding:NSUTF8StringEncoding];

// Hash key (hexa decimal) string data length.
NSMutableData *hashKeyData = [NSMutableData dataWithLength:CC_SHA1_DIGEST_LENGTH];

// Key Derivation using PBKDF2 algorithm.
int result = CCKeyDerivationPBKDF(kCCPBKDF2, passwordData.bytes, passwordData.length, saltData.bytes, saltData.length, kCCPRFHmacAlgSHA1, 1000, hashKeyData.mutableBytes, hashKeyData.length);

// Hexa decimal or hash key string from hash key data.
NSString *hexDecimalString = hashKeyData.description;

NSLog(@"Hexa decimal string:%@", hexDecimalString);
票数 7
EN

Stack Overflow用户

发布于 2011-12-20 09:48:28

你是在为iOS5构建吗?或者更早的版本?

头文件中定义的CCKeyDerivationPBKDFCCCalibratePBKDF都只在IOS5 (或OSX10.7)和更高版本上可用。

您可以通过在终端窗口中执行以下命令来确保该文件存在:

代码语言:javascript
运行
复制
$ find /Developer/ -name CommonKeyDerivation.h
/Developer//Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS5.0.sdk/usr/include/CommonCrypto/CommonKeyDerivation.h
/Developer//Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator5.0.sdk/usr/include/CommonCrypto/CommonKeyDerivation.h
/Developer//SDKs/MacOSX10.7.sdk/usr/include/CommonCrypto/CommonKeyDerivation.h
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8569555

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档