Skip to content

Lesson 1 of 4

Hashing and salts

Listen to an AI summary of Hashing and salts

0:00 / 0:35

What is a hash

A website checks a password at every sign-in, so the website saves something for each password. A website that saves the password as typed gives every password to anyone who steals a copy of the database. A safer website saves a hash, a value of fixed length worked out from the password.

A hash function is the code that works out a hash, and working out a hash is called hashing. A hash function works like a blender. The fruit is the password and the smoothie is the hash.

The same fruit always makes the same smoothie, and nobody can get the fruit back. The same password always gives the same hash, and nobody can work out the password from the hash.

How an attacker guesses a password

An attacker guesses the password instead. The attacker takes a common password such as password1, works out the hash and looks for that hash in the stolen database. A match shows which customers use password1.

Hash functions such as MD5, SHA-1 and SHA-256 are made to be fast. One graphics card, the part of a computer that draws games, lets an attacker try billions of SHA-256 guesses a second.

A hash that is slow on purpose

A password hashing function makes a hash slowly on purpose, so that guessing many passwords takes a long time. One hash can take a tenth of a second, which a customer who signs in does not notice. An attacker who makes a billion guesses one at a time needs about three years.

A work factor is a setting that decides how slow a password hashing function is. A work factor set too low makes the function fast again. Four password hashing functions are Argon2id, scrypt, bcrypt and PBKDF2.

A password saved with a fast hash function is the weakness named use of a password hash with insufficient computational effort (CWE-916).

Two customers with the same password

Sam and Priya, two customers of the sports shop, both choose the password password1.

  1. sam 0b14d501a594442a...the hash of password1
  2. priya 0b14d501a594442a...the same hash
No salt.

The two hashes are the same, so an attacker who finds one password has found both. A rainbow table holds, in a compressed form, a list of common passwords worked out in advance together with the hash of each password. An attacker looks up each saved hash in a rainbow table and skips the guessing.

A salt is random data added to each password before hashing, so that two people with the same password get different hashes. In the blender, a salt is one extra ingredient that is different for each person.

Some code uses one salt for every customer, such as the word SportsShop. The code hashes password1SportsShop.

  1. sam b82a3146ce897cea...the hash of password1SportsShop
  2. priya b82a3146ce897cea...the same hash
One salt shared by every customer.

An existing rainbow table has no hash of password1SportsShop. The two hashes still match, and one new rainbow table works for every customer of the shop.

Safer code makes a new random salt for each customer and saves the salt beside the hash.

  1. sam q7Lm2xVd 17cb75f9c73502e4...Sam's salt, then the hash of password1q7Lm2xVd
  2. priya k3Tz91Qm 849aac1acba78e07...Priya's salt, then the hash of password1k3Tz91Qm
A salt for each customer.

The two hashes differ, so nothing shows that Sam and Priya share a password. A salt is not a secret. An attacker who reads the salts still has to start the guessing again for each customer.

  1. No salt

    password1 One existing rainbow table finds every common password

  2. more work for the attacker

    One salt for every customer

    password1SportsShop One new rainbow table finds every common password

  3. more work again

    A random salt for each customer

    password1q7Lm2xVd The attacker needs a new table for each customer

What the code hashes in each case.

No salt is use of a one-way hash without a salt (CWE-759). One salt for every customer is use of a one-way hash with a predictable salt (CWE-760).

What the code looks like

The first example is JavaScript from the sports shop's sign-up page. crypto is a library that comes with Node, and MD5 is a fast hash function. Look at what goes into the hash function.

// Weakness: a fast hash function and no salt
const saved = crypto.createHash('md5').update(password).digest('hex')

createHash('md5') picks the hash function MD5. Only password goes into update, so the code adds no salt. digest('hex') returns the hash as letters and digits.

The safer example uses argon2, a library for Argon2id. The first line loads the library. Look at the two lines that hold argon2.

const argon2 = require('argon2')

// Safer: slow on purpose, with a new random salt each time
const saved = await argon2.hash(password)                  // at sign-up
const matches = await argon2.verify(saved, typedPassword)  // at sign-in

argon2.hash makes a new random salt, makes the hash slowly, and returns the salt and the hash as one string. argon2.verify checks a typed password against the saved string. The word await waits for the slow work to finish.

Comparing two secret values

The JavaScript operator === stops at the first character that differs. The time a comparison takes can therefore show an attacker how much of a guess is right.

A constant-time comparison takes the same time whether or not the two values match, such as crypto.timingSafeEqual(a, b) in JavaScript. The function needs two values of the same length, each in the form Node calls a Buffer, so safer code hashes both values first and compares the two hashes. Safer code compares a hash, a message authentication code (MAC) and a token that works more than once this way. A token proves who a person is (Signed tokens), and a MAC is a hash made with a key (Cryptography). The verify function of a password hashing library compares in constant time.

Examples of what to look for

  • A password that goes into a fast hash function, such as createHash('md5'), createHash('sha1') or createHash('sha256') in JavaScript.
  • A work factor below the published minimum, such as bcrypt with a cost of 4.
  • A salt that is the same for every customer, such as salt = "SportsShop".
  • A salt shorter than 16 bytes, or made with Math.random(), whose numbers can be predicted.
  • A hash function written by the application's own programmers in place of a library.
  • A hash, a MAC or a token that works more than once compared with ===, as in if (token === savedToken).

Check your understanding

  1. State two properties of a hash.

    Show the answer

    The same data always gives the same hash, and nobody can work out the data from the hash. A fixed length is also right.

  2. True or false. With a salt for each customer, Sam and Priya have the same saved hash.

    Show the answer

    False. The two salts differ, so the two hashes differ.

  3. Give one reason why a fast hash function is the wrong choice for a password.

    Show the answer

    An attacker with a copy of the database can try billions of guesses a second.

Practise it

Challenges with a weakness this lesson teaches.