【问题标题】:Why hash_equals and password_verify are not working properly?为什么 hash_equals 和 password_verify 不能正常工作?
【发布时间】:2018-03-02 10:47:57
【问题描述】:

在我的登录页面中,password_verify 后出现错误,好像我在验证密码时使用了 hash_equals。需要知道原因。

第二个问题是每次我通过更改密码页面更改密码时 hash_equals 不验证密码。以下是代码

if (!password_verify($password, $user['password'])) {
    $errors[]='Password does not match';
}

if (!hash_equals($password, $user['password'])) {
    $errors[]='Password does not match';
}

【问题讨论】:

  • 这两个功能的手册你都看过了吗?

标签: php hash passwords


【解决方案1】:

函数 hash_equals() 并不是要使用哈希验证密码,这是 password_verify() 函数的工作,所以不要在代码中使用 hash_equals():

// Hash a new password for storing in the database.
// The function automatically generates a cryptographically safe salt.
$hashToStoreInDb = password_hash($_POST['password'], PASSWORD_DEFAULT);

// Check if the hash of the entered login password, matches the stored hash.
// The salt and the cost factor will be extracted from $existingHashFromDb.
$isPasswordCorrect = password_verify($_POST['password'], $existingHashFromDb);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-16
    • 2019-01-04
    • 2020-09-03
    • 2016-10-10
    • 2016-10-24
    • 2017-02-27
    • 2017-07-08
    • 2014-11-23
    相关资源
    最近更新 更多