Why hash_equals and password_verify are not working properly?

The function hash_equals() is not meant to verify a password with a hash, that’s the job of the password_verify() function, so don’t use hash_equals() in your code:

// 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);

Leave a Comment