Salting Passwords

4.4. Salting Passwords#

Given enough time and computational power it is possible to recover original passwords from cryptographic hashes. One common method is to precompute the hash value for common passwords. An attacker can then check the hash values against these precomputed values for a match.

To solve this problem, we can prepend or append a salt, which is a random value, to the start or end of the original password. This changes the output of the hash function.

Therefore an attacker would need to precompute hash values for each unique salt value, which is equal to the number of users. This additional layer of complexity makes the task of recovering original passwords for all users impractical.

For example:

  1. The user registers with the password "mypassword123"

  2. The server generates a random salt value e.g. "3948ry10Xp"

  3. The server hashes the salted password i.e. hash("mypassword1233948ry10Xp") generating a hash e.g. "a9f3b8c2d6e7f1"

  4. The hash value and salt are stored in the database

We can see how this change is reflected in a User table:

Before

User ID

Username

Password

1

alice123

342sckjn812123

2

bob_smith

s0d9sucsxicbn9

3

admin

cgasd8g123e9hs

After

User ID

Username

Password

Salt

1

alice123

a9f3b8c2d6e7f1

3948ry10Xp

2

bob_smith

z4h7x9m2p8q3v6

m8t4z2q7x1

3

admin

y5n3d8w2k6t4r9

k5r3y8n2v6