`password_verify`调用返回false以获得正确的密码 [英] `password_verify` call returning false for correct password

查看:189
本文介绍了`password_verify`调用返回false以获得正确的密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码片段:

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





$ b

在一台服务器上工作正常(呜呼!),在另一台服务器上工作正常工作。 我刚刚将它放在codepad.org上,它也在那里失败。



问题在于(可以在该键盘页面上看到),由 crypt 计算出的散列长度为13,而不是所需的60。 / p>

我在github上使用 ircmaxel的password_compat库实现PHP 5.5只有 password_verify 函数。

解决方案

你正在运行PHP版本小于5.3.7的脚本,因此算法'2y'还不知道。



如果可能的话,我会考虑做这个服务器上的PHP升级,'2y'参数解决了unicode输入字符串的问题。



如果这不是一个选项,那么您可以替换兼容包。在线49的某处你会发现...

$ p $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ );

...将其更改为前BCrypt常数'2a'...

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

...这当然不是最优的,但它是您在早期版本中可以做的最好的。
$ b 新生成的密码哈希现在将以'$ 2a $ 10 $ ...'开头,验证这个散列值应该适用于每个系统。


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.

解决方案

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.

这篇关于`password_verify`调用返回false以获得正确的密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆