iOS5上的CCKeyDerivationPBKDF

Fog*_*ter 5 security iphone objective-c ios commoncrypto

我正在尝试在我的应用程序中编写密码加密功能,遵循本文.

我编写了一个运行该CCCalibratePBKDF函数并输出轮数的函数.

const uint32_t oneSecond = 1000;
uint rounds = CCCalibratePBKDF(kCCPBKDF2,
                               predictedPasswordLength,
                               predictedSaltLength,
                               kCCPRFHmacAlgSHA256,
                               kCCKeySizeAES128,
                               oneSecond);
Run Code Online (Sandbox Code Playgroud)

这很有效,但是当我尝试实现下一部分时,一切都会出错.

我可以开始编写CCKeyDerivationPBKDF函数调用,它会自动完成函数和所有参数.当我通过填充它所有参数也自动完成.

- (NSData *)authenticationDataForPassword: (NSString *)password salt: (NSData *)salt rounds: (uint) rounds
{
    const NSString *plainData = @"Fuzzy Aliens";
    uint8_t key[kCCKeySizeAES128] = {0};
    int keyDerivationResult = CCKeyDerivationPBKDF(kCCPBKDF2,
                                                   [password UTF8String],
                                                   [password lengthOfBytesUsingEncoding: NSUTF8StringEncoding],
                                                   [salt bytes],
                                                   [salt length],
                                                   kCCPRFHmacAlgSHA256,
                                                   rounds,
                                                   key,
                                                   kCCKeySizeAES128);
    if (keyDerivationResult == kCCParamError) {
        //you shouldn't get here with the parameters as above
        return nil;
    }
    uint8_t hmac[CC_SHA256_DIGEST_LENGTH] = {0};
    CCHmac(kCCHmacAlgSHA256,
           key,
           kCCKeySizeAES128,
           [plainData UTF8String],
           [plainData lengthOfBytesUsingEncoding: NSUTF8StringEncoding],
           hmac);
    NSData *hmacData = [NSData dataWithBytes: hmac length: CC_SHA256_DIGEST_LENGTH];
    return hmacData;
}
Run Code Online (Sandbox Code Playgroud)

但是一旦我击中; 它标志着一个错误说"没有匹配函数来调用'CCKeyDerivationPBKDF'"并且它不会构建或任何东西.

我导入了CommonCrypto/CommonKeyDerivation.h和CommonCrypto/CommonCryptor.h,因为这两个都是枚举名称所必需的.

Fog*_*ter 2

是的,我找到了问题(和解决方案)。

因为我使用的是 ZXing,所以我必须将 .m 文件重命名为 .mm,以便它可以运行 ZXing 库中的 C++ 内容。

我不知道为什么,但以这种方式重命名文件会破坏 CCKeyDerivationPBKDF 功能。

我现在已将加密代码移至其自己的类中,并将其保留为 .m,现在我需要的只是包含两个导入,就像我在原始帖子中所做的那样。

我不必包含任何框架或任何东西。