PBKDF2-Wrapper 提供了一个非常简单的 Objective-C 接口,用于 CommonCrypto 实现的 PBKDF2 算法。
从密码生成加密密钥。 PBKDF2Result
将自动处理创建安全的随机盐并调整迭代次数,以大约 100ms 的时间生成密钥。
NSString *password = ...;
PBKDF2Result *result = [[PBKDF2Result alloc] initWithPassword:password];
NSData *encryptionKey = result.derivedKey;
之后,您可以方便地归档配置,其中包含有关密钥长度、盐、迭代次数以及生成密钥时使用的伪随机函数等信息。
[NSKeyedArchiver archiveRootObject:result.configuration
toFile:@"/path/to/file"];
下次启动时,您可以从归档配置文件中检索该文件,并在生成密钥时使用。
PBKDF2Configuration *configuration = [NSKeyedUnarchiver unarchiveObjectWithFile:@"/path/to/file"];
NSString *password = ...;
PBKDF2Result *result = [[PBKDF2Result alloc] initWithPassword:password
configuration:configuration];
NSData *encryptionKey = result.derivedKey;
您还可以使用多个有用的初始化器显式地创建配置。以下是一个示例
NSData *salt = ...;
[[PBKDF2Configuration alloc] initWithSalt:salt
derivedKeyLength:16
rounds:20000
pseudoRandomFunction:PBKDF2PseudoRandomFunctionSHA256];
通过 CocoaPods 安装
pod 'PBKDF2-Wrapper'
导入公共头文件
#import <PBKDF2-Wrapper/PBKDF2-Wrapper.h>
要运行测试,请拉取仓库并运行以下命令:
$ bundle install
$ bundle exec rake test:prepare
$ bundle exec rake
PBKDF2-Wrapper 采用 MIT 许可证。有关更多信息,请参阅 LICENSE 文件。