I am storing a username and an encrypted password in a mysql database. For testing only, I am also storing the password in an unencrypted form in the database as well.
In the following code, I get the hashed password and the unencrypted password from the database. I then encrypt the unencrypted password.
The given password does not pass the password verification test for the stored hash or the new hash.
The stored password does pass the password verification test for both the stored hash and the new hash.
The call to strcmp says that the stored password and the given password are equal.
How could this be?
[edit] : I am passing in $password from user input on the web page.
// get hashed password from database
$sql = "SELECT member_password FROM member WHERE member_username=:username;";
$stmt = $db->prepare($sql);
$stmt->bindParam("username", $username);
$stmt->execute();
$hash = $stmt->fetch(PDO::FETCH_ASSOC);
$hash = $hash["member_password"];
// get unencrypated password from database
$sql = "SELECT member_unencrypted FROM member WHERE member_username=:username;";
$db = getConnection();
$stmt = $db->prepare($sql);
$stmt->bindParam("username", $username);
$stmt->execute();
$unencrypted = $stmt->fetch(PDO::FETCH_ASSOC);
$unencrypted = $unencrypted["member_unencrypted"];
// encrypt the unencrypted password that was retrieved from the database
$encrypted = password_hash($unencrypted, PASSWORD_DEFAULT);
// given password does not pass the new hash per this test
if(password_verify($password, $encrypted))
echo '<br>given password passed new hash';
else
echo '<br>given password did not pass new hash';
// stored password does pass the new hash per this test
if(password_verify($unencrypted, $encrypted))
echo '<br>stored password passed new hash';
else
echo '<br>stored password did not pass new hash';
// given password does not pass the stored hash per this test.
if(password_verify($password, $hash)){
echo '<br>given password passed stored hash';
else
echo '<br>given password did not pass stored hash';
// stored password does pass the stored hash per this test.
if(password_verify($unencrypted, $hash))
echo '<br>stored password passed stored hash';
else
echo '<br>stored password did not pass stored hash';
// stored and given passwords are equal per this test.
if(strcmp($unencrypted, $password))
echo '<br>stored and given passwords are equal';
else
echo '<br>stored and given passwords are not equal';
Output:
given password did not pass new hash
stored password passed new hash
given password did not pass stored hash
stored password passed stored hash
stored and given passwords are equal