我使用Zend'Crypt'Password'Bcrypt
存储在数据库中加密的密码。但是现在我仔细看了一下,我似乎不理解这个类的验证方法:
/**
* Verify if a password is correct against a hash value
*
* @param string $password
* @param string $hash
* @throws Exception'RuntimeException when the hash is unable to be processed
* @return bool
*/
public function verify($password, $hash)
{
$result = crypt($password, $hash);
return Utils::compareStrings($hash, $result);
}
根据注释"根据哈希值验证密码是否正确"的功能
但是当我检查php crypt函数时,它正在调用第二个参数是可选的$salt
而不是$hash
字符串来验证。
我如何阅读这个:它首先使用传递的$hash
作为盐来加密我们想要检查的$password
,然后它比较它用作盐的相同$hash
与加密的$result
!
我在这里错过了什么?要么php-doc是不正确的,要么我不理解发生了什么,要么我在文档中错过了一些东西。
Bcrypt散列具有良好的文档结构,例如:
$2y$10$aPk2mEEIkGonq6/JGr0OKOhYOdgomu61ARBjDLgb0UmHM4L8f7Hxe
String $2y$
为前缀,10
为成本,aPk2mEEIkGonq6/JGr0OKO
为盐(128位,base64编码22个字符),hYOdgomu61ARBjDLgb0UmHM4L8f7Hxe
为结果散列。
crypt
函数可以识别这种格式,并使用它的适当部分作为盐,因此将整个哈希作为第二个参数传递是没有问题的。