PHP 5.4 检查两个密码验证


PHP 5.4 check both Password Verify

我的电脑上有PHP版本5.4,所以我不能使用password_hash(),而是改用crypt()

但下一个问题是如何验证密码是否匹配?

PHP文档说,如果你使用crypt()最好的方法用作密码转换器是hash_equals()但只有PHP 5.5。

我总是得到:

致命错误:hash_equals在...

正如你提到的,php 5.4 还没有这个函数,所以你有两个选项来利用这个非常有用的函数:

更好的方法:

更新到 php 5.5/5.6 并使用当时的本机库

如果无法做到这一点:

您可以使用此库:

https://github.com/ircmaxell/password_compat

该库提供与 PHP 5.5 正在处理的 password_* 函数的向前兼容性。

你可以像这样比较它们:

<?php 
$the_password= crypt('12345', 'aasddas');
$user_entered_pass='125';
if(crypt($user_entered_pass,'aasddas') == $the_password){
    echo "right";
}else{
    echo "wrong";
}
?>

简短的回答是这就是password_compat的用途,所以使用它。

PHP文档说,如果你使用crypt()最好的密码器方法是hash_equals()但只有PHP 5.5。

  1. 如果您遵循理智的建议,那么您不是在crypt(),而是在password_verify()中使用password_hash()
  2. hash_equals()是在 PHP 5.6 中添加的,而不是 5.5。