0

I am generating a password in PHP as follows:

$options = [
    'cost' => 11,
];
// Get the password from post
$passwordFromPost = $_POST['password'];

$hash = password_hash($passwordFromPost, PASSWORD_BCRYPT, $options);

and I insert it in a MySQL table.

Now, I would like to retrieve it. I was using a hash+salt password, but I would like to remove the salt option. How could I retrieve the created password in PHP?

5
  • A simple SELECT hash FROM table query should fetch it. You shouldn't be mucking around with the automatic salts. I feel like I'm missing something in your question. Commented Jun 9, 2017 at 18:04
  • 6
    You can't get the password back, if that's what you're asking -- hashes are one-way. Use password_verify() to check if user-supplied input matches your stored value. Commented Jun 9, 2017 at 18:04
  • You want to un-salt a hashed password? You can't. That's the whole point of using salted hashes. If you can do it, so can potential attackers. Commented Jun 9, 2017 at 18:04
  • you cant, compare string with same hash to database Commented Jun 9, 2017 at 18:04
  • You very badly need to do some research on how to properly store passwords. You're operating under some dangerous misconceptions that need to be fixed before you continue. No, I won't listen them, because I doubt I could catch them all, and if I gave you an incompetent checklist, that could just be worse, because now you think you know all the pitfalls. Instead, find a detailed guide on how to do it, and Google for any reasoning that isn't explained in the article. Commented Jun 9, 2017 at 18:14

2 Answers 2

2

Ok, here it goes. You cannot retrieve the raw passwords again as hash(hash+salt) being the one way encryption technique(and that makes sense one should not be able to read anybody's raw password and misuse). The way it work is, when user type in their passphrase to login, the same encryption algorithm(that's been followed while storing) being followed to create the hash out of it. The comparison now happens between hash to hash to get a valid session token.

Now, coming to the question if you want to change the encryption algorithm or the salt, you need to allow user to login with the old encryption algo. What you should be doing is to gradual migration of old user's password hash to new password hash. And the only option you get the raw pass is when user type them in. You need to identify if they are the old users, if they are allow them log-in with backward compatible algorithm and silently update their password with encrypted with the new salt.

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

Comments

0

You can't retrieve the plaintext password.

But If in your case the web site is up and people are using it, you can add a function after password verify to update user's password in the database using your new hash. You might be able to know which passords are updated by simply adding a field in your database to tell you.

1 Comment

it is not "unfortunate" that you can't retrieve plaintext passwords. It's intended.

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.