`password_verify` call returning false for correct password

穿精又带淫゛_ 提交于 2019-12-20 03:48:27

问题


I have the following snippet of code:

// bcrypt hash of 'password'
$hash = '$2y$10$4u0cQ.WEnwHDo.C5Nl1vm.shKA0beQ32wqzphSfzklAq9OcDM2nLu';
if(password_verify('password', $hash)) {
    print_r('woohoo!');
}
else {
    print_r('fubar');
}

On one server it's working fine (woohoo!), on another it doesn't work. I've just put it up on codepad.org and it fails there too.

The problem is (as can be see on that codepad page) that the hash computed by crypt is of length 13 instead of the required 60.

I'm using ircmaxel's password_compat library on github to implement the PHP 5.5 only password_verify function.


回答1:


It seems that you are running the script on a PHP version smaller than 5.3.7, and therefore the algorithm '2y' is not yet known.

If possible, i would consider to do a PHP upgrade on this server, the '2y' parameter solves a problem with unicode input strings.

Should this not be an option, then you can replace the algorithm in the compatibility pack. Somewhere about line 49 you will find...

$hash_format = sprintf("$2y$%02d$", $cost);

...change it to the former BCrypt constant '2a'...

$hash_format = sprintf("$2a$%02d$", $cost);

...this is of course not optimal, but it is the best you can do on earlier versions.

A new generated password hash will now start with '$2a$10$...' and the verification with this hash-value should work on every system.



来源:https://stackoverflow.com/questions/15090481/password-verify-call-returning-false-for-correct-password

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