我有存储在密码database
哈希与DefaultPasswordHasher
在add
行动。
我还有另一项更改登录用户密码的操作,在此表单上,我有一个字段current_password
,需要与中的当前密码值进行比较database
。
问题是,DefaultPasswordHasher
每次我对表单的值进行哈希处理时,都会生成一个不同的哈希,因此这将永远与数据库中的哈希不匹配。
请遵循“ current_password”字段的验证代码:
->add('current_password', 'custom', [
'rule' => function($value, $context){
$user = $this->get($context['data']['id']);
if ($user) {
echo $user->password; // Current password value hashed from database
echo '<br>';
echo $value; //foo
echo '<br>';
echo (new DefaultPasswordHasher)->hash($value); // Here is displaying a different hash each time that I post the form
// Here will never match =[
if ($user->password == (new DefaultPasswordHasher)->hash($value)) {
return true;
}
}
return false;
},
'message' => 'Você não confirmou a sua senha atual corretamente'
])
这就是bcrypt的工作方式。Bcrypt是一种更强大的密码哈希算法,它将根据当前系统的熵为同一值生成不同的哈希值,但是它能够比较原始字符串是否可以哈希为已经哈希的密码。
要解决您的问题,请使用check()
函数而不是hash()
函数:
->add('current_password', 'custom', [
'rule' => function($value, $context){
$user = $this->get($context['data']['id']);
if ($user) {
if ((new DefaultPasswordHasher)->check($value, $user->password)) {
return true;
}
}
return false;
},
'message' => 'Você não confirmou a sua senha atual corretamente'
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句