How Password Hashing Works and Why Salt Matters
When you create an account on a website, you type a password. A well-built service never saves that password. Instead, it stores a transformed version that cannot be turned back into the original. If the database leaks, attackers get scrambled values rather than a list of everyone's passwords.
The transformation is called hashing, and doing it properly involves more than running a single function. This article explains how hashing works, what salt adds, and which approaches developers should choose.
What a hash function does
A hash function takes input of any length and produces a fixed-size output called a hash or digest. Good hash functions share a few properties.
- Deterministic. The same input always yields the same output.
- One-way. You cannot work backward from the output to the input.
- Sensitive to change. Altering one character produces a completely different hash.
- Collision resistant. It is extremely hard to find two different inputs with the same output.
Here is a small example using the SHA-256 function, which is a general-purpose hash:
echo -n "hello" | sha256sum
The result is a long string of hexadecimal characters. Change hello to hellp and the string changes entirely.
How login works with hashes
When you register, the server hashes your password and saves the result. When you log in later, it hashes what you typed and compares the two hashes. If they match, the password was correct. The server never needs the original password, and it never needs to store it.
This is very different from encryption. Encryption is designed to be reversed with a key. Password storage should not be reversible at all, so a stolen database offers no key to turn the data back into passwords.
The attacker's methods
If hashes cannot be reversed, how do attackers crack them? They guess. They take a list of likely passwords, hash each one, and look for matches in the stolen data.
- Dictionary attacks try common words and leaked passwords from earlier breaches.
- Brute force tries every possible combination up to a certain length.
- Rainbow tables are huge precomputed lists that map hashes back to passwords, so a lookup replaces the work of hashing.
Because many people choose the same popular passwords, a single guess can match thousands of accounts if each user's hash is computed the same way.
What salt adds
A salt is a random value generated for each user and combined with the password before hashing. It is stored alongside the hash and does not need to be secret.
Salting solves two problems:
- Identical passwords produce different hashes. If two users both choose the same password, their stored values still differ because their salts differ. An attacker cannot spot shared passwords by scanning for repeated hashes.
- Precomputed tables become useless. A rainbow table would have to be rebuilt for every possible salt, which defeats the whole point of precomputing.
With salt, attackers must attack each account separately. That raises their cost considerably, though it does not stop a determined guesser from attacking a single weak password.
A related term is pepper, a secret value added to all passwords and kept outside the database, for example in application configuration or a hardware module. It is an optional extra layer, and unlike salt, it must stay secret.
Why speed is the enemy
General-purpose hashes such as SHA-256 are designed to be fast. That is a virtue for checking file integrity, but a flaw for passwords. A modern graphics card can compute enormous numbers of fast hashes every second, making guessing cheap.
Password hashing algorithms are deliberately slow and often memory-hungry. They include a work factor that lets you tune the cost as hardware improves. The slowdown is barely noticeable to a real user who logs in once, but it multiplies the effort needed for an attacker making billions of guesses.
Algorithms to use
Current guidance from security organizations favors purpose-built password hashing functions.
- Argon2, especially the Argon2id variant, which won an open password-hashing competition and is often the first recommendation for new systems.
- scrypt, which is memory-hard and well studied.
- bcrypt, an older design that remains widely used and acceptable, with a length limit on input worth knowing about.
- PBKDF2, a standardized option that is useful where compliance requirements apply, though it is less resistant to specialized hardware.
Avoid plain MD5, SHA-1 or unsalted SHA-256 for passwords. They were never intended for this job.
Practical guidance for developers
- Use a well-maintained library for your language rather than assembling your own scheme.
- Let the library generate the salt. Most modern functions create it and embed it in the stored string.
- Pick a work factor that makes a single hash take a noticeable fraction of a second on your servers, and review it periodically.
- Plan for upgrades. Store the algorithm and parameters with each hash, so you can rehash to a stronger setting when a user next logs in.
- Enforce reasonable password rules, such as a minimum length and a check against known leaked passwords.
- Encourage multi-factor authentication, since hashing protects stored data but not a password that was phished.
Guidance for users
You cannot see how a website stores your password, so protect yourself regardless. Use a long, unique password for each site, ideally generated by a password manager. Reuse is what turns one breach into many.
Frequently Asked Questions
Is hashing the same as encryption?
No. Encryption is reversible by anyone with the correct key, while a password hash is designed to be one-way. Encrypting passwords creates the risk that whoever steals the key can read them all, which is why hashing is preferred for storing logins.
Does the salt need to be kept secret?
No. A salt works because it is unique and random, not because it is hidden. It is normally stored next to the hash. Its purpose is to force attackers to work on each password individually, and that remains true even when they know the salt.
Why do some sites say they can email me my password?
A site that can send your original password is storing it in a recoverable form, which is poor practice. A properly designed service can only offer a reset link, because it truly does not know your password. Treat the first kind of site with extra caution and never reuse that password elsewhere.