Basically, you have two options that vary in complexity:
- Store the hash of the user's registered password using the hash algorithm of your choice (more on this later).
- create a random salt (permanent, secret line) that will be used with the user's password to create a hash as described above, and then save this hash in the database.
When you retrieve a user record, you compare the hash computed from the provided password with the hash stored in the database.
Example:
$HashedPass = hash('sha512', $password);
or with the predefined SALT:
$HashedPass = hash('sha512', $password.SALT_STRING);
Save this in the database as before.
Authentication is performed similarly:
$HashedPass = hash('sha512', $password.SALT_STRING);
and then get from the database based on this hash comparison with the saved one.
Now I would like to dwell on your problems with Hash algorithms: You do not need to use md5, you can also use more secure hash algorithms, see Comment here: PHP hash function One of the suggestions is to use sha512 algorithm.
Most importantly, you should understand that a hash is a one way conversion - there is no practical way to reverse engineer the original password only from the hash, perhaps only find alternative strings that produce the same hash string.
I hope you find that you use a strong Hash algorithm along with salt to mitigate the damage of a stolen Hash DB that is suitable enough for your needs.
source share