加盐哈希函数
function bcryptHash($rawPassword, $round = 8)
{if($round < 4 || $round > 32) $round = 8;
$salt = '$2a$' . str_pad($round, 2, '0', STR_PAD_LEFT) . '$';
$randomValue = openssl_random_pseudo_bytes(16);
//echo base64_encode($randomValue);
$salt .= substr(strtr(base64_encode($randomValue), '+', '.'), 0, 22);
//echo $salt;
return crypt($rawPassword, $salt);
}
//用户输入输入放入密码$password
$password = 'zhanghongmin';
$hashedPassword = bcryptHash($password);
//将哈希过后的密码存入数据库中
下面的是验证函数
function bcryptVerify($rawPassword, $storedHash){
return crypt($rawPassword, $storedHash) == $storedHash;
}
//根据ID获取用户的$hashedPassword,然后将两个密码代入验证函数进行验证
if(bcryptVerify($password, $hashedPassword)){
echo 'ok';
};