【问题标题】:PBKDF2 using CommonCrypto on iOS在 iOS 上使用 CommonCrypto 的 PBKDF2
【发布时间】:2012-01-24 01:38:58
【问题描述】:

我正在尝试使用CommonCrypto 生成使用PBKDF2 的密钥,但我似乎无法导入CommonCrypto/CommonKeyDerivation.h,我只是错误地找不到它。

有什么想法吗?

编辑:我可能应该提到我已经添加了安全框架,我可以导入所有其他 CommonCrypto 标头。

【问题讨论】:

    标签: objective-c ios pbkdf2 commoncrypto


    【解决方案1】:

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

    #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);
    

    【讨论】:

    • 请记住,如果您只需要在一个设备(或至少同一类设备)上派生密钥,PBKDF 校准可能是可以的。当你例如需要在不同设备上同步数据并派生相同的密钥,然后设置多个轮次以在所有设备(例如 Mac Pro 和 iPhone)上轻松工作是一种更明智的方法。 10000-20000 之间的数字在 2012 年应该是一个不错的数字。
    • 最好使用 SecRandomCopyBytes() 在密码学应用程序中生成伪随机数。 - 否则,很棒的代码!我喜欢 PBKDF2 轮次估计位 =)
    • 是否需要创建一个长度为 32 字节的盐?
    • 嗨@PiyushKashyap,256 位盐可确保 256 位密钥的最大熵。无论如何,这是完全没有必要的。 128位绰绰有余,64位就够了。
    【解决方案2】:
    1. 将此库添加到您的项目 libcommonCrypto.dylib
    2. #import 到散列密钥生成类中。
    3. 使用以下代码生成哈希键。

    这是我使用的代码:

    // 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);
    

    【讨论】:

    • 很好,刚刚更新。在 iOS8 项目中不需要添加 libcommonCrypto.dylib
    • 不要使用 .description 进行数据到字符串的转换。
    【解决方案3】:

    您正在为 iOS5 构建吗?还是更早的版本?

    头文件中定义的 API CCKeyDerivationPBKDFCCCalibratePBKDF 仅在 IOS5(或 OSX 10.7)及更高版本上可用。

    您可以通过在终端窗口中执行此操作来确保文件存在:

    $ 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
    

    【讨论】:

    • 不幸的是iOS 4。我想我会寻找一个替代的实现。
    猜你喜欢
    • 2012-02-24
    • 1970-01-01
    • 2022-01-14
    • 1970-01-01
    • 1970-01-01
    • 2011-07-28
    • 1970-01-01
    • 2012-04-08
    • 2018-06-23
    相关资源
    最近更新 更多