code q&a
Resources About
Question:

What is a salt?

Lots of people use the same password, so a site needs a way to store passwords that doesn’t give away who shares one. A salt is a random string that the site makes up for each user and mixes into their password before scrambling it.

Sites shouldn’t store your actual password, so they store a hash of it and compare hashes when you log in. The problem is that the same password always hashes to the same output, so anyone who steals the database can spot every user who picked “password123” at a glance. Here’s “password123” for two users run through SHA-256 with different salts:

salt a8f2c1 + password123 → 97a9cc509f78d93a0138d2a7a113a9894a6309cd4046f143fa5755bcdc98ede7
salt 7d4e90 + password123 → acf933b43dc58aee25bcccdc55c2ab267cc8f4a5f022700ddd0486917e85a782

The salt isn’t a secret. The site saves it in plain text next to the hash because it needs the same salt to check your password when you log in. Attackers build rainbow tables, which are giant lists of common passwords already hashed ahead of time, so they can look up stolen hashes instantly. Salts make those lists useless and force an attacker to crack each account separately.

A salt won’t save a weak password, though. Someone going after your account can still grab your salt from the leak and start guessing. Slow password hashes like bcrypt and Argon2 handle that by taking a noticeable fraction of a second per guess, where MD5 or SHA-256 can run billions of times per second.

I’d never write salting code by hand because bcrypt and Argon2 generate a fresh salt for every password and store it in the same string as the hash.

You might also like