其他人已經表明它是如何完成的,所以對一般的原則......
像已經通過註釋中的NullUserException鏈接,如果安全性很重要,則不應該通過向md5添加靜態鹽來實現。 Note however that you can store your salt as-is。靜鹽和md5都不被認爲是非常安全的方法。如果沒有那麼重要,它們就沒關係,但如果它確實如此,你就應該走另一條路。
About static salt:
常見的錯誤是使用相同的鹽在每個散列。鹽 被硬編碼到程序中,或者隨機生成一次。這是 無效,因爲如果兩個用戶具有相同的密碼,他們仍然會有 具有相同的散列。攻擊者仍然可以使用反向查找表 攻擊同時對每個散列運行字典攻擊。他們 只需要將salt應用於每個密碼猜測之前,他們就會散列 它。如果鹽硬編碼到一個受歡迎的產品,查找表 和彩虹表可建爲鹽,使之更容易被產品產生裂紋 哈希值。
一個新的隨機鹽必須在每次用戶創建一個帳戶 或改變其密碼時生成。
如果鹽是靜態的,攻擊者可以爲所有可能性生成一個彩虹表。用鹽對用戶來說是獨一無二的,這樣做毫無意義。
MD5是專爲計算速度,所以它的fundamentally not the way to go。另外,它已經有很多pre-created rainbow tables了,還有很多online cracking tools。
鏈接的頁面也包含PHP source code,作爲如何正確完成此操作的示例。
<?php
/*
* Password hashing with PBKDF2.
* Author: havoc AT defuse.ca
* www: https://defuse.ca/php-pbkdf2.htm
*/
// These constants may be changed without breaking existing hashes.
define("PBKDF2_HASH_ALGORITHM", "sha256");
define("PBKDF2_ITERATIONS", 1000);
define("PBKDF2_SALT_BYTES", 24);
define("PBKDF2_HASH_BYTES", 24);
define("HASH_SECTIONS", 4);
define("HASH_ALGORITHM_INDEX", 0);
define("HASH_ITERATION_INDEX", 1);
define("HASH_SALT_INDEX", 2);
define("HASH_PBKDF2_INDEX", 3);
function create_hash($password)
{
// format: algorithm:iterations:salt:hash
$salt = base64_encode(mcrypt_create_iv(PBKDF2_SALT_BYTES, MCRYPT_DEV_URANDOM));
return PBKDF2_HASH_ALGORITHM . ":" . PBKDF2_ITERATIONS . ":" . $salt . ":" .
base64_encode(pbkdf2(
PBKDF2_HASH_ALGORITHM,
$password,
$salt,
PBKDF2_ITERATIONS,
PBKDF2_HASH_BYTES,
true
));
}
function validate_password($password, $good_hash)
{
$params = explode(":", $good_hash);
if(count($params) < HASH_SECTIONS)
return false;
$pbkdf2 = base64_decode($params[HASH_PBKDF2_INDEX]);
return slow_equals(
$pbkdf2,
pbkdf2(
$params[HASH_ALGORITHM_INDEX],
$password,
$params[HASH_SALT_INDEX],
(int)$params[HASH_ITERATION_INDEX],
strlen($pbkdf2),
true
)
);
}
// Compares two strings $a and $b in length-constant time.
function slow_equals($a, $b)
{
$diff = strlen($a)^strlen($b);
for($i = 0; $i < strlen($a) && $i < strlen($b); $i++)
{
$diff |= ord($a[$i])^ord($b[$i]);
}
return $diff === 0;
}
/*
* PBKDF2 key derivation function as defined by RSA's PKCS #5: https://www.ietf.org/rfc/rfc2898.txt
* $algorithm - The hash algorithm to use. Recommended: SHA256
* $password - The password.
* $salt - A salt that is unique to the password.
* $count - Iteration count. Higher is better, but slower. Recommended: At least 1000.
* $key_length - The length of the derived key in bytes.
* $raw_output - If true, the key is returned in raw binary format. Hex encoded otherwise.
* Returns: A $key_length-byte key derived from the password and salt.
*
* Test vectors can be found here: https://www.ietf.org/rfc/rfc6070.txt
*
* This implementation of PBKDF2 was originally created by https://defuse.ca
* With improvements by http://www.variations-of-shadow.com
*/
function pbkdf2($algorithm, $password, $salt, $count, $key_length, $raw_output = false)
{
$algorithm = strtolower($algorithm);
if(!in_array($algorithm, hash_algos(), true))
die('PBKDF2 ERROR: Invalid hash algorithm.');
if($count <= 0 || $key_length <= 0)
die('PBKDF2 ERROR: Invalid parameters.');
$hash_length = strlen(hash($algorithm, "", true));
$block_count = ceil($key_length/$hash_length);
$output = "";
for($i = 1; $i <= $block_count; $i++) {
// $i encoded as 4 bytes, big endian.
$last = $salt . pack("N", $i);
// first iteration
$last = $xorsum = hash_hmac($algorithm, $last, $password, true);
// perform the other $count - 1 iterations
for ($j = 1; $j < $count; $j++) {
$xorsum ^= ($last = hash_hmac($algorithm, $last, $password, true));
}
$output .= $xorsum;
}
if($raw_output)
return substr($output, 0, $key_length);
else
return bin2hex(substr($output, 0, $key_length));
}
閱讀:[你應該知道密碼安全性](http://crackstation.net/hashing-security.htm) – NullUserException
謝謝我正在通過它 – deerox
我想建議不要使用md5,而是更多現代哈希算法。 – Aleph