CakePHP 3 DefaultPasswordHasher

早过忘川 提交于 2021-02-05 06:10:30

问题


I am working on creating an authentication form that has a password and password_again field in it. So I can check to make sure they match, I am using the beforeSave() function of my UsersTable to take care of the hashing. As a test, I have just set it to show me the password_again, its hashed result and then die.

    public function beforeSave($event, $entity){
        debug($entity->password_again);
        $hasher = new DefaultPasswordHasher();
        $entity->password_again = $hasher->hash($entity->password_again);
        debug($entity->password_again);
        die();
    }

I am curious as to why the hash keeps changing every time I reload the results. I would have expected the hash to stay the same each time I reloaded it. Thanks in advance.

EDIT: So it turns out that you should not hash both passwords and then try to compare them. Instead, the DefaultPasswordHasher->check(password_again, password_hash) will verify if the passwords match for you.


回答1:


The DefaultPasswordHasher uses PHP's password_hash function which by default uses blowfish algorithm with a different salt each time, resulting in different hash on every invocation.




回答2:


public function checkPassword($passedPassword, $actualPassword) {
    if ((new DefaultPasswordHasher)->check($passedPassword, $actualPassword)) {
        return true;
    } else {
        return false;
    }
}


来源:https://stackoverflow.com/questions/25837332/cakephp-3-defaultpasswordhasher

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!