为什么hash_equals和password_verify无法正常工作?

问题描述:

在我的登录页面中,我收到password_verify后的错误,如果我使用hash_equals验证密码。需要知道原因。为什么hash_equals和password_verify无法正常工作?

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

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

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

您是否阅读过关于这两个函数的手册? – Mjh

功能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);