你有一些重大錯誤,但這個想法是可靠的。
我在my Github repository OpenSSL的PBKDF2示例代碼(以及PolarSSL和各種其他的),但重要的例子將(使用PBKDF2-HMAC-SHA-512,作爲最佳的選擇):
void PBKDF2_HMAC_SHA_512(const char* pass, const unsigned char* salt, int32_t iterations, uint32_t outputBytes, char* hexResult, uint8_t* binResult)
{
unsigned int i;
unsigned char digest[outputBytes];
PKCS5_PBKDF2_HMAC(pass, strlen(pass), salt, strlen(salt), iterations, EVP_sha512(), outputBytes, digest);
for (i = 0; i < sizeof(digest); i++)
{
sprintf(hexResult + (i * 2), "%02x", 255 & digest[i]);
binResult[i] = digest[i];
};
}
而且,它還將被稱爲:
// 2*outputBytes+1 is 2 hex bytes per binary byte, and one character at the end for the string-terminating \0
char hexResult[2*outputBytes+1];
memset(hexResult,0,sizeof(hexResult));
uint8_t binResult[outputBytes];
memset(hexResult,0,sizeof(binResult));
PBKDF2_HMAC_SHA_512(pass, salt, iterations, outputBytes, hexResult, binResult);
嘗試使用SHA-384或SHA-512作爲基準散列函數;它們包括64位操作,可以減少大多數基於GPU的攻擊者的優勢。
使用大量(數十萬次到數萬次)的迭代次數。 SHA-1甚至更大。
我也有一個大的列表test vectors in my Github repository - 你可以使用它們來驗證你的代碼是否返回它應該的結果。
請注意,您不應使用該尺寸的鹽。您應該只在表示以空字符結尾的字符數組上使用'strlen',而不是數組。 –
@Professor:*「我只有兩個編譯錯誤,如下所示......」 - 請複製並粘貼您收到的* exact *錯誤消息,包括編譯器用於指示問題表達式的小刻度標記。你的'pass'和'salt'是非空間垃圾。 'keylen'應該是字節,而不是位。 – jww