2011-12-20 7 views
13

PBKDF2を使用してCommonCryptoのキーを生成しようとしていますが、私はCommonCrypto/CommonKeyDerivation.hをインポートできないようです。iOS上でCommonCryptoを使用しているPBKDF2

アイデア?

編集:セキュリティフレームワークをすでに追加してあり、他のすべてのCommonCryptoヘッダーをインポートできます。

答えて

32

は、ここで私は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); 
+1

PBKDFキャリブレーションは、1つのデバイス(または少なくとも同じクラスのデバイス)でキーを派生する必要がある場合にのみ問題ありません。たとえばデータを同期して別のデバイスで同じキーを取得する必要がある場合は、すべてのデバイス(Mac ProやiPhoneなど)で問題なく動作するラウンド数を設定する方が賢明なアプローチです。 2012年に10000-20000の間の何かが良い数字になるはずです。 –

+6

暗号化アプリケーションでの擬似乱数生成には、SecRandomCopyBytes()を使用する方が良いです。 - そうでなければ、素晴らしいコード!私はPBKDF2ラウンド推定ビット=が好きです –

+0

長さ32バイトの塩を作る必要がありますか? –

1

iOS5用にビルドしていますか?またはそれ以前のバージョン?

ヘッダーファイルに定義されているAPI、CCKeyDerivationPBKDFおよびCCCalibratePBKDFは、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 
+0

のiOS 4を、残念ながら。私は代わりの実装を探します。 – AnthonyM

6
  1. libcommonCrypto.dylib
  2. プロジェクトにこのライブラリを追加
  3. ハッシュ鍵生成クラスには#import。
  4. 次のコードを使用してハッシュキーを生成します。

これは私が使用しているどのようなコードです:

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

正確に、うまくいった。 – magichero

+0

偉大な、ちょうど更新。 iOS8のプロジェクトにlibcommonCrypto.dylibを追加する必要はありません – kraag22

+0

とても良いコードです。 –

関連する問題