Jump to content

Is 'password_hash' broken?

Recommended Posts

Posted (edited)

I copied the code for password_hash at php.net:

 * In this case, we want to increase the default cost for BCRYPT to 12.
 * Note that we also switched to BCRYPT, which will always be 60 characters.
$options = [
    'cost' => 12,
echo password_hash("rasmuslerdorf", PASSWORD_BCRYPT, $options);

and changed it for use in my login page:


	$options = ['cost' => 12,];
    $user = mysqli_real_escape_string($db_link,$_GET['username']);
    $pass = password_hash($_GET['password'], PASSWORD_BCRYPT, $options);

but my page keeps saying invalid user/pass. Upon echoing the $pass I find that the result changes EACH time. so I created a test page that runs the code from php.net (verbatim code) 20x and I got:




Is password_hash broken? or am I mistaken to think that it's supposed to return the same output everytime fror the same input?

Edited by Karaethon
typos corrected

Share this post

Link to post
Share on other sites

password_hash() is used when the hash of a password is saved, i.e. during registration/password changes, and the hash it produces for a given input is different each time since a random salt is generated and used each time it is called. password_verify() is used to test if a submitted password corresponds to a saved hash.

Share this post

Link to post
Share on other sites

Ahhh. ok... so I use _hash when the user registers or edits their password and _verify when they log in? Kinda wish the php documentation said something about that, or at least I didn't catch it if it did.

Share this post

Link to post
Share on other sites

There isn't a tutorial on password hashing, if that's what you mean, but the documentation for password_hash does say it creates passwords, and that the returned value can be used with password_verify() to verify the hash.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.