Password Hashing and Credential Storage
Why passwords are never stored as plaintext, why fast hashes like SHA-256 are the wrong tool, and why bcrypt/scrypt/argon2 exist.
Password Hashing and Credential Storage
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- A password should never be stored in a form that lets anyone read it back — not plaintext, not reversible encryption — because any database leak, backup, or rogue insider then exposes every user's real password directly.
- A general-purpose fast hash like SHA-256 is the wrong tool for passwords: it's engineered for speed, which is exactly the property an attacker doing offline guessing wants, and modern GPUs can test billions of SHA-256 guesses per second.
- A salt — a random value stored alongside each password's hash — stops an attacker from using one precomputed table (a rainbow table) against every user at once, and stops two users with the same password from producing the same hash.
- bcrypt, scrypt, and argon2 are deliberately slow and, for scrypt/argon2, deliberately memory-hungry, which raises the cost of checking each guess for an attacker without meaningfully slowing down the one login check your own server does per request.
- Verifying a password at login never means decrypting the stored value — it means re-hashing the submitted password with the same salt and parameters, then comparing the two hashes.
The problem you’re actually solving
Every signup form eventually asks the same question a database designer has to answer: what do you actually put in the password column? The naive answer — just store what the user typed — fails the instant your database is ever read by someone who shouldn’t read it: a backup that leaks, a SQL injection bug, a disgruntled employee with read access, a misconfigured S3 bucket. All of these happen regularly to real companies. If the password column holds the real password, every single one of those incidents instantly hands over every user’s actual login credential — and because people reuse passwords across sites, it hands over access to their email, their bank, and whatever else they reused that password on.
So the design goal isn’t “prevent the database from ever leaking” — you should assume it eventually will, the same way you assume a network link will eventually drop a packet. The goal is: when it leaks, the attacker gets something they can’t turn back into the original password, at least not without serious effort. That’s what hashing is for, and it’s worth being precise about what a hash is before touching the fancier algorithms.
Hashing, and why the obvious hash is wrong
A hash function takes an input of any size and produces a fixed-size output, deterministically — the same input always produces the same output — and in a way that’s effectively irreversible: you can’t go from the output back to the input. That one-way property is exactly what you want for passwords. Store hash(password) instead of password, and verifying a login means hashing the submitted password again and comparing hashes, not decrypting anything. The embedded video above argues for a specific modern answer to “which hash function” — argon2id — and makes a detail worth sitting with: argon2id generates its own salt automatically, which is a different thing from a pepper, a second, static secret value (kept outside the database, in your application config) that you add on top. A pepper protects against the specific case where the hash database leaks but your application’s configuration doesn’t; it’s an optional extra layer, not a replacement for the salt.
Here’s where it gets subtle: hash functions like SHA-256 or MD5 are designed to be fast, because most of their uses — verifying a file wasn’t corrupted, building a hash table, computing a git commit ID — benefit from speed with no downside. For passwords, speed is a liability, not a feature. If your server can compute a SHA-256 hash in a microsecond, so can an attacker who steals your hash database, and they get to try billions of candidate passwords per second on a single consumer GPU. Most real passwords come from a small effective space — dictionary words, common patterns, previous leaks — so billions of guesses per second cracks the majority of a stolen database in hours, sometimes minutes. MD5 is even worse: it’s not just fast, it has known cryptographic weaknesses that make it actively unsuitable for anything security-sensitive. Using a fast general-purpose hash for passwords is a specific, well-documented mistake, not just a theoretical one — it shows up constantly in post-breach disclosures.
Salting: defeating the precomputed attack
Before getting to slow hashing, there’s a second problem even a slow hash doesn’t fix by itself: if two users pick the same password, they get the same hash. That means an attacker doesn’t need to crack your database live — they can precompute a giant table mapping common passwords to their hashes once (a “rainbow table”) and then just look up every hash in your stolen database against that table instantly. Precomputation turns a hard per-password problem into a one-time cost amortized across every leaked database that uses the same hash function.
A salt fixes this: a random value, unique per user, that gets mixed into the password before hashing and stored alongside the resulting hash (the salt itself doesn’t need to be secret — it just needs to be unique). Now hash(password + salt) differs for every user even if their passwords match, and a precomputed table built for one salt is useless against any other. Every modern password-hashing library generates and stores the salt for you automatically as part of the hash string — you don’t manage it by hand, but you should understand why it’s there.
The real answer: deliberately slow, deliberately expensive hashing
Salting defeats precomputation, but it doesn’t touch the raw-speed problem: an attacker with a stolen, salted hash database can still brute-force each hash individually at GPU speed, one salt at a time. The fix the industry actually settled on is to use hash functions built specifically for passwords, which are deliberately slow and often deliberately memory-hungry:
- bcrypt — the longest-established of the three, built around a tunable “cost factor” that controls how many rounds of internal computation it runs. Doubling the cost factor roughly doubles the time to compute one hash. You raise the cost factor over the years as hardware gets faster, so the same relative difficulty holds.
- scrypt — adds a second dimension: memory. It’s designed so that computing one hash requires a meaningful chunk of RAM, which specifically blunts the advantage of GPUs and custom ASIC hardware, both of which are great at raw parallel computation but comparatively starved for per-core memory.
- argon2 — the newest of the three, winner of a public password-hashing competition, and the one most current guidance (including OWASP’s, and the embedded video above) recommends by default for new systems. Like scrypt, it’s memory-hard, and it has a specific variant,
argon2id, tuned to resist both GPU-parallel attacks and certain side-channel attacks.
The shared idea across all three: instead of fighting attacker hardware advantages with cleverness, make every single hash computation expensive enough — in time, in memory, or both — that doing it a few times per login is a non-issue, but doing it billions of times to crack a stolen database is a multi-year project instead of an afternoon. That’s the entire design philosophy in one sentence, and it’s why “just use bcrypt” (or argon2, today) became the standard advice instead of “roll your own hashing scheme.”
In practice, you almost never implement any of this by hand. Every mainstream language has a maintained library — bcrypt in Node, passlib or argon2-cffi in Python, golang.org/x/crypto/bcrypt in Go — that generates the salt, applies the algorithm with sane default parameters, and gives you a single string to store and a single function call to verify against it. The job of a backend developer here isn’t implementing argon2’s internals; it’s knowing why you reach for one of these libraries instead of hashlib.sha256(password).hexdigest(), and not getting talked out of it by “but it’s faster.”
What this lesson isn’t
Everything above is about what happens to the credential itself, at rest, the moment someone submits a password. It says nothing about how a server recognizes a user on their next request without asking for the password again — that’s the job of sessions, cookies, and tokens, covered in JWT, API Keys, and Session Auth. The two problems get conflated constantly because they show up in the same login flow, but they’re genuinely separate concerns with separate failure modes: a session-token bug leaks active logins, a password-hashing bug leaks the credential itself, permanently, for every account in the database.
Check yourself
Why is storing a password with reversible encryption (rather than plaintext) still a bad idea?
Reversible encryption means there's a key somewhere that turns the stored value back into the real password, and anyone who gets that key — through the same leak, misconfiguration, or insider access that exposes the database — gets every password back in readable form. A proper password hash has no key and no decryption path at all.
Why is SHA-256 a poor choice for hashing passwords even though it's a cryptographically strong hash function?
SHA-256 is strong at its actual job — producing a unique fingerprint quickly and detecting tampering — but 'quickly' is the problem for passwords. An attacker who steals a database of SHA-256 hashes can test billions of candidate passwords per second on commodity GPU hardware, because the hash was never designed to resist being computed at scale.
What problem does a salt solve that hashing alone doesn't?
Without a salt, two users with the same password get the same hash, and an attacker can precompute a lookup table of common password hashes once and use it against every account in every leaked database that uses the same hash function. A unique random salt per password forces the attacker to attack each hash individually, making precomputed tables useless.
What does it mean for bcrypt or argon2 to be 'deliberately slow,' and why doesn't that make login annoying for real users?
These algorithms are tuned to take a fixed, adjustable amount of compute time (and, for argon2 and scrypt, memory) per hash — maybe 100-300 milliseconds. A real user only hashes a password once per login, so that delay is invisible. An attacker trying millions of guesses pays that same per-guess cost millions of times, which turns a brute-force attack from feasible to impractically slow.
What actually happens when a server checks a login attempt against a stored bcrypt hash?
The server reads the salt and cost parameters embedded in the stored hash string, re-runs bcrypt on the submitted password using those same inputs, and compares the resulting hash to the one in the database. There is no decryption step — the stored hash is never reversed, only recomputed and compared.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.