use*_*884 4 php hash crypt blowfish
我确实对crypt()PHP函数感到困惑.
当第二个crypt明显使用不同的第二个参数时,以下两个crypt函数如何提供相同的输出?Diff salt意味着diff hash对吧?
echo crypt("password", '$2y$09$anexamplestringforsalt$')."\n<br>";
echo crypt("password", crypt("password", '$2y$09$anexamplestringforsalt$'))."\n<br>";
Run Code Online (Sandbox Code Playgroud)
输出:
$2y$09$anexamplestringforsale/.K.VdgECUVEd9N4ja3u1WtgPi5BXZq
Run Code Online (Sandbox Code Playgroud)
irc*_*ell 11
原因是因为salt是crypt提供的哈希输出的一部分.
$2y$09$anexamplestringforsale/.K.VdgECUVEd9N4ja3u1WtgPi5BXZq
Run Code Online (Sandbox Code Playgroud)
这分为几个部分:
2y - 算法标识符(bcrypt)09 - 成本参数anexamplestringforsale - 盐/.K.VdgECUVEd9N4ja3u1WtgPi5BXZq - 哈希这导致了良好的属性,只是能够直接使用结果哈希作为验证调用中的salt.
$hash = crypt($password, $salt);
if ($hash === crypt($password, $hash)) {
Run Code Online (Sandbox Code Playgroud)
现在您不需要分别存储算法,成本或盐.只需将它们直接存储在哈希结果中即可.简单.
另外,我强烈建议您使用简化的密码哈希API,它专门用于缓解这些问题:password_hash().