Password Hashing issue with password_hash & password_verify

773 views Asked by At

I currently upgraded my server to PHP 5.5 and want to make good use of the new functions password_hash and password_verify.

I cant seem to get a hash to be verified correctly? I have copied the exact examples from the PHP manual and it still seems to be returning false?

Is their something I am missing?

$hash = password_hash("rasmuslerdorf", PASSWORD_DEFAULT)."\n";


if (password_verify('rasmuslerdorf', $hash)) {
    echo 'Password is valid!';
} else {
echo 'Invalid password.';
}

returns

Invalid password.
2

There are 2 answers

3
Marc B On BEST ANSWER

You're appending a \n to your hash, which CHANGES the hash:

$hash = password_hash("rasmuslerdorf", PASSWORD_DEFAULT)."\n";
                                                         ^^^^---here

Eliminate that, and it'll start working.

2
Daniel A. White On

I think the problem is that you are appending a new line with '\n' at the end.

They are using that to add a newline to the output in the examples.