Today I've shared how I have generated MD5 hash with Salt in php to secure passwords.

Salt

As most common MD5 hashes can easily be found in Rainbow table, it's better to make your original text hard to guess or to be super common. This we can achieve by adding a Salt. In encryption or hashing, a salt is a random text that we are going to add into the password.

Generating MD5 Hash in PHP

You can generate MD5 hash like this

md5('your text here')

How we are going to generate hash for our password

md5('Your Password + A Random Text As Salt')

Verify the password

We are going to store the generated hash in database. Next time when your enter the password for login, we will generate hash in a same way (user input password + our salt).

We will fetch the saved hash from database and match with this new hash. If they both matches, user input was a valid password for login.

Caution

Now a days, MD5 is not being considered as super good option for hashing and storing password. This example was an experiment for my learning.

In case if you are looking more secure solution, please check SHA-256 algorithm.