4
<?php

require 'password.php';
$hash1 = password_hash('testpassword',PASSWORD_BCRYPT,array('cost' => 11));
$hash2 = password_hash('testpassword',PASSWORD_BCRYPT,array('cost' => 11));

if(password_verify($hash1,$hash2)) echo 'Pass';
else echo 'Fail';

?>

I'm trying to use bcrypt provided by the password_compat library with PHP 5.4.16, but this script always outputs "Fail" even though it's comparing two hashes of the same password, why?

Edit - Just for clarification, I realize the hashes aren't identical, otherwise I'd just compare them instead of using a function.

1
  • This is the point of hashing. No two are alike. I find using Blowfish makes life easier too Commented Feb 24, 2014 at 2:43

1 Answer 1

14

You need to pass the password and the hash to password_verify():

password_verify('testpassword', $hash1)

Note: testpassword is password without hash

References:

PS: password_hash generates different results expectedly, since it contains a random salt

Sign up to request clarification or add additional context in comments.

2 Comments

Gah, can't believe I overlooked that, I saw that they used a variable in the example and I assumed it was another hash. Thanks a lot, I'll accept the answer in a few minutes when it lets me.
@Cains That's why I use a specific PHP IDE - It suggests me a function with its parameters, like this: "password_verify(password: string, hash: string) bool"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.