Hashing and Salting Explained With a Working Example

Why sites store hashes instead of passwords, what a salt actually does, and why the fast hashes you know are the wrong tool for the job.

The problem hashing solves

A website needs to check your password without keeping a copy of it. If it kept copies, every breach would hand attackers a list of working credentials, and every employee with database access could read them. The solution, in use since the 1970s, is to store a hash instead.

A cryptographic hash function takes any input and produces a fixed-size output with three properties:

  • Deterministic: the same input always gives the same output, so the site can hash what you type at login and compare.
  • One-way: there is no way to compute the input from the output except by guessing inputs and checking.
  • Avalanche: changing one bit of input changes about half the output bits, so similar passwords have unrelated hashes.

Try it on the SHA-256 generator: hashword and hashworD share nothing.

Why a plain hash is not enough

Suppose a site stores SHA-256(password). Two problems appear the moment the database leaks.

Identical passwords give identical hashes. The attacker sorts the table, sees that 40,000 users share one hash, and knows before cracking anything that it is probably 123456. Crack it once, own 40,000 accounts.

Precomputation works. Because the hash of 123456 is the same on every site that uses SHA-256, attackers keep tables of hashes for billions of common passwords, called rainbow tables, and crack by lookup instead of computation.

What a salt does

A salt is a random value, typically 16 bytes, generated for each user when they set their password and stored next to the hash in plain view. The site hashes salt + password instead of the password alone.

Now three users with the same password have three unrelated hashes, and a precomputed table is worthless because it would need an entry for every password times every possible salt. The attacker must attack each user’s hash from scratch. The salt is not secret; its job is uniqueness, not concealment.

Why fast hashes are still wrong

Salting fixes precomputation and duplicate detection. It does nothing about speed. SHA-256 was designed to hash gigabytes of data quickly, and a modern GPU computes ten billion of them per second. Against a leaked table of salted SHA-256 hashes, an attacker still tries every eight-character password in minutes.

A password hash needs the opposite property: it should be slow, and ideally slow in a way that gets slower as hardware improves. That is what bcrypt, scrypt and Argon2 provide. They run the hash thousands of times, or demand large amounts of memory, so that each guess costs milliseconds instead of nanoseconds. For a user logging in once, milliseconds are invisible. For an attacker making billions of guesses, they are decisive: the same attack that took minutes now takes centuries.

Our guide to bcrypt vs Argon2 vs scrypt compares them; the short version is that any of the three is fine and any plain SHA is not.

A working example

Here is what happens when you set the password sunshine1 on a site that does this properly, using bcrypt:

  1. The server generates 16 random bytes of salt.
  2. It runs bcrypt with a cost factor of 12, meaning 4,096 rounds of the internal key setup, on the salt and password. This takes about 250 ms.
  3. It stores one string that contains the algorithm, the cost, the salt and the hash: $2b$12$R9h/cIPz0gi.URNNX3kh2OPST9/PgBkqquzi.Ss7KIUgO2t0jWMUW

At login, the server reads the cost and salt out of that string, hashes what you typed the same way, and compares. Generate a few on the bcrypt tool: each run gives a different hash for the same password, and each verifies.

What you cannot do

You cannot “decrypt” a hash, and a site that emails you your old password on request is storing it in plain text. Change that password and, if you can, leave.

Hashing beyond passwords

The same functions that are wrong for passwords are right for everything else: verifying that a download is intact (file checksum), fingerprinting documents, building Git commit ids, signing messages with a key (HMAC), and mining Bitcoin. The distinction is whether the input is a low-entropy secret a human chose. If it is, slow it down. If it is not, use SHA-256 and enjoy the speed.

New tools and guides, about once a month.

No tracking pixels, no upsells. Unsubscribe with one click.

Newsletter