PHP密码的安全哈希和salt
目前称MD5部分不安全。考虑到这一点,我想知道使用哪种机制来保护密码 这个问题,, 建议多次散列可能是个好主意,而建议使用盐 我正在使用PHP。我想要一个安全快速的密码加密系统。对密码进行一百万次哈希运算可能更安全,但速度也较慢。如何在速度和安全之间取得良好的平衡?另外,我更希望结果具有恒定数量的字符PHP密码的安全哈希和salt,php,security,passwords,hash,protection,Php,Security,Passwords,Hash,Protection,目前称MD5部分不安全。考虑到这一点,我想知道使用哪种机制来保护密码 这个问题,, 建议多次散列可能是个好主意,而建议使用盐 我正在使用PHP。我想要一个安全快速的密码加密系统。对密码进行一百万次哈希运算可能更安全,但速度也较慢。如何在速度和安全之间取得良好的平衡?另外,我更希望结果具有恒定数量的字符 散列机制必须在PHP中可用 它必须是安全的 它可以使用盐(在这种情况下,所有的盐都一样好吗?有没有办法产生好的盐?) 另外,我是否应该在数据库中存储两个字段(例如,一个使用MD5,另一个使用SHA
免责声明:这个答案写在2008年 从那时起,PHP就给了我们,并且,从他们的介绍开始,他们就是推荐的密码散列和检查方法 尽管如此,答案的理论仍然是一本很好的读物 TL;博士 不应该做的
- 不要限制用户可以输入的密码字符。只有傻瓜才会这么做
- 不要限制密码的长度。如果你的用户想要一个包含SuperCalifragilisticExpialidious的句子,不要阻止他们使用它
- 不要删除或转义密码中的HTML和特殊字符
- 切勿以纯文本形式存储用户密码
- 不要通过电子邮件向用户发送密码,除非用户丢失了密码,并且您发送了临时密码
- 永远不要以任何方式记录密码
- 永远不要使用或MD5甚至SHA256散列密码!每秒可以超过600亿和1800亿哈希(分别)
- 不要混用,使用十六进制输出或base64_编码。(这适用于任何可能含有流氓
的输入,这会严重削弱安全性。)\0
- 尽可能使用scrypt;B如果你不能,请回答
- 如果不能使用bcrypt或scrypt,请使用PBKDF2和SHA2哈希
- 数据库受损时重置每个人的密码
- 实现合理的8-10个字符的最小长度,并且至少需要1个大写字母、1个小写字母、一个数字和一个符号。这将提高密码的熵,从而使密码更难破解。(有关一些讨论,请参阅“什么是好密码?”部分。)
$t_hasher = new PasswordHash(8, FALSE);
$hash = $t_hasher->HashPassword($password);
$t_hasher = new PasswordHash(8, FALSE);
$check = $t_hasher->CheckPassword($password, $hash);
<?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));
}
?>
$password = 'anna';
$hash = password_hash($password, PASSWORD_DEFAULT);
$expensiveHash = password_hash($password, PASSWORD_DEFAULT, array('cost' => 20));
password_verify('anna', $hash); //Returns true
password_verify('anna', $expensiveHash); //Also returns true
password_verify('elsa', $hash); //Returns false
/**
* Generating string
* @param $size
* @return string
*/
function Uniwur_string($size){
$text = md5(uniqid(rand(), TRUE));
RETURN substr($text, 0, $size);
}
/**
* Hashing string
* @param $string
* @return string
*/
function hash($string){
return hash('sha512', $string);
}
// generating unique password
$password = Uniwur_string(20); // or you can add manual password
// generating 32 character salt
$salt = Uniwur_string(32);
// now we can manipulate this informations
// hashin salt for safe
$hash_salt = hash($salt);
// hashing password
$hash_psw = hash($password.$hash_salt);