1
$\begingroup$

I'm researching how to do salt and hashing. I'll be using SHA256. Here is what I found:

To Store a Password

  • Generate a long random salt using a CSPRNG.

  • Prepend the salt to the password and hash it with a standard password hashing function like Argon2, bcrypt, scrypt, or PBKDF2.

  • Save both the salt and the hash in the user's database record.

To Validate a Password

  • Retrieve the user's salt and hash from the database.
  • Prepend the salt to the given password and hash it using the same hash function.
  • Compare the hash of the given password with the hash from the database. If they match, the password is correct. Otherwise, the password is incorrect.

My question is: How do you get the original salt after it's been hashed to the user's password in the database? You can't "unhash" the salt and add that to the password they've entered and then hash. Wouldn't you get a different result?

$\endgroup$
2
  • 2
    $\begingroup$ You store it in step 3 $\endgroup$
    – eckes
    Nov 13, 2017 at 7:17
  • $\begingroup$ also, look up Modular Crypt Format and PHC String Format $\endgroup$ Nov 13, 2017 at 13:11

1 Answer 1

5
$\begingroup$

Did you read what you found? Quote: Save both the salt and the hash in the user's database record. and Retrieve the user's salt and hash from the database.

So, you simply store the original salt you used to the database, from where you can later read it. There is no need to "unhash" anything as you have the original salt available at all times… stored next to the salted hash in your database.

More practical:

Let's say for some user you use salt 123 and hash the user password with that salt. You then store both 123 and the hash result to your database:

ID   | salt | hashResult
-----+------+-------------
1    | 123  | F7A4…

When you later want to compare some password, you read the stored salt 123 and hash the password you want to compare using that salt you have previously stored in your database.

In simpler words, this roughly boils down to:

Does "User input hashed with stored salt 123" produce the same result as "the stored hash value"? If the answer is "yes" because the resulting hash matches the hash value you previously stored, the passwords match… if the answer is "no" because both hash results are different, the passwords don't match.

I guess what you've missed is that you always have the salt available — stored next to the related password hash. If you wouldn't do that, you would indeed not be able to recover the salt later on.

Also note that storing the salt in your database next to the hash result isn't a problem because a salt is not a "secret", while the password is a "secret".

For more information on what a salt is and what it does, check "Can you help me understand what a cryptographic “salt” is?"

$\endgroup$

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.