gpt4 book ai didi

ios - 使用 HMAC SHA1 的 CCKeyDerivationPBKDF 经常返回 -1

转载 作者:可可西里 更新时间:2023-11-01 17:16:55 24 4
gpt4 key购买 nike

当我使用 CCKeyDerivationPBKDF 时来自 #import <CommonCrypto/CommonKeyDerivation.h> , 它返回 -1这是一个未定义的结果。我不知道我是否错过了什么。如果我使用 HMAC MD5 它可以返回成功或其他算法,并且仅当我使用 HMAC SHA1 时它将返回错误状态。

int feedback = CCKeyDerivationPBKDF(kCCPBKDF2, clearTextData.bytes, clearTextData.length, secretData.bytes, secretData.length, kCCHmacAlgSHA1, 2048, result, sizeof(result)); 

最佳答案

我注意到您正在使用 kCCHmacAlgSHA1而不是 kCCPRFHmacAlgSHA1 ,这可能是错误。

这对我有用:

NSData *keyData = [@"password" dataUsingEncoding:NSUTF8StringEncoding];
NSData *salt = [@"salt" dataUsingEncoding:NSUTF8StringEncoding];
uint rounds = 2048;
uint keySize = kCCKeySizeAES128;

NSMutableData *derivedKey = [NSMutableData dataWithLength:keySize];

CCKeyDerivationPBKDF(kCCPBKDF2, // algorithm
keyData.bytes, // password
keyData.length, // passwordLength
salt.bytes, // salt
salt.length, // saltLen
kCCPRFHmacAlgSHA1, // PRF
rounds, // rounds
derivedKey.mutableBytes, // derivedKey
derivedKey.length); // derivedKeyLen

NSLog(@"derivedKey: %@", derivedKey);

输出:
derivedKey: <2c13cb7a d3468748 5c3f3d4f 18ebddbd>

Swift 3

let password = "TestPassword"
let salt = Data("salt" .utf8);
let keySize = kCCKeySizeAES128;
let rounds = 2048

var derivedKeyData = Data(repeating:0, count:keySize)

let derivationStatus = derivedKeyData.withUnsafeMutableBytes {derivedKeyBytes in
salt.withUnsafeBytes { saltBytes in

CCKeyDerivationPBKDF(
CCPBKDFAlgorithm(kCCPBKDF2),
password, password.utf8.count,
saltBytes, salt.count,
UInt32(kCCPRFHmacAlgSHA1),
UInt32(rounds),
derivedKeyBytes,
derivedKeyData.count)
}
}

print("derivedKeyData: \(derivedKeyData.map { String(format: "%02hhx", $0) }.joined())")

输出:
derivedKeyData: 75d1f85fd64d170b3ffe66c7f1d1519a

来自已停用文档部分的更通用的解决方案:

Password Based Key Derivation 2 (Swift 3+)

基于密码的 key 派生既可用于从密码文本派生加密 key ,也可用于保存密码以进行身份​​验证。

可以使用多种哈希算法,包括此示例代码提供的 SHA1、SHA256、SHA512。

rounds 参数用于降低计算速度,这样攻击者就必须在每次尝试上花费大量时间。典型的延迟值在 100 毫秒到 500 毫秒之间,如果性能 Not Acceptable ,可以使用更短的值。

这个例子需要通用加密
必须要有项目的桥接头:
#import <CommonCrypto/CommonCrypto.h>
添加Security.framework到项目。

参数:

password     password String  
salt salt Data
keyByteCount number of key bytes to generate
rounds Iteration rounds

returns Derived key


func pbkdf2SHA1(password: String, salt: Data, keyByteCount: Int, rounds: Int) -> Data? {
return pbkdf2(hash:CCPBKDFAlgorithm(kCCPRFHmacAlgSHA1), password:password, salt:salt, keyByteCount:keyByteCount, rounds:rounds)
}

func pbkdf2SHA256(password: String, salt: Data, keyByteCount: Int, rounds: Int) -> Data? {
return pbkdf2(hash:CCPBKDFAlgorithm(kCCPRFHmacAlgSHA256), password:password, salt:salt, keyByteCount:keyByteCount, rounds:rounds)
}

func pbkdf2SHA512(password: String, salt: Data, keyByteCount: Int, rounds: Int) -> Data? {
return pbkdf2(hash:CCPBKDFAlgorithm(kCCPRFHmacAlgSHA512), password:password, salt:salt, keyByteCount:keyByteCount, rounds:rounds)
}

func pbkdf2(hash :CCPBKDFAlgorithm, password: String, salt: Data, keyByteCount: Int, rounds: Int) -> Data? {
let passwordData = password.data(using:String.Encoding.utf8)!
var derivedKeyData = Data(repeating:0, count:keyByteCount)

let derivationStatus = derivedKeyData.withUnsafeMutableBytes {derivedKeyBytes in
salt.withUnsafeBytes { saltBytes in

CCKeyDerivationPBKDF(
CCPBKDFAlgorithm(kCCPBKDF2),
password, passwordData.count,
saltBytes, salt.count,
hash,
UInt32(rounds),
derivedKeyBytes, derivedKeyData.count)
}
}
if (derivationStatus != 0) {
print("Error: \(derivationStatus)")
return nil;
}

return derivedKeyData
}

示例用法:

let password     = "password"
//let salt = "saltData".data(using: String.Encoding.utf8)!
let salt = Data(bytes: [0x73, 0x61, 0x6c, 0x74, 0x44, 0x61, 0x74, 0x61])
let keyByteCount = 16
let rounds = 100000

let derivedKey = pbkdf2SHA1(password:password, salt:salt, keyByteCount:keyByteCount, rounds:rounds)
print("derivedKey (SHA1): \(derivedKey! as NSData)")

示例输出:

derivedKey (SHA1): <6b9d4fa3 0385d128 f6d196ee 3f1d6dbf>

Password Based Key Derivation Calibration (Swift 3+)

确定用于当前平台上特定延迟的 PRF 轮数。

一些参数默认为代表性值,不会对轮数产生实质性影响。

password Sample password.  
salt Sample salt.
msec Targeted duration we want to achieve for a key derivation.

returns The number of iterations to use for the desired processing time.


func pbkdf2SHA1Calibrate(password: String, salt: Data, msec: Int) -> UInt32 {
let actualRoundCount: UInt32 = CCCalibratePBKDF(
CCPBKDFAlgorithm(kCCPBKDF2),
password.utf8.count,
salt.count,
CCPseudoRandomAlgorithm(kCCPRFHmacAlgSHA1),
kCCKeySizeAES256,
UInt32(msec));
return actualRoundCount
}

示例用法:

let saltData       = Data(bytes: [0x73, 0x61, 0x6c, 0x74, 0x44, 0x61, 0x74, 0x61])
let passwordString = "password"
let delayMsec = 100

let rounds = pbkdf2SHA1Calibrate(password:passwordString, salt:saltData, msec:delayMsec)
print("For \(delayMsec) msec delay, rounds: \(rounds)")

示例输出:

For 100 msec delay, rounds: 93457

关于ios - 使用 HMAC SHA1 的 CCKeyDerivationPBKDF 经常返回 -1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26675279/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com