Every time there's news that "a site's database leaked", people ask first: did my password leak too? The answer depends on one decision a developer at that site made. Was the password stored as it is, or in a form that can't be turned back into the password?
This lesson is about the second choice: storing a password without ever storing the real one. Imagine you want to check someone's house key without keeping the key. You keep only a photo of the pattern of its teeth. With that photo you can match a key that arrives, but you cannot rebuild the key from the photo. In an app, the key is the password, and the photo is the hash: a one-way fingerprint of the password.
Don't store plain text
If the database leaks and passwords are stored as they are (plain text, text anyone can read right away), every account is open to the attacker at once. Worse, many people use the same password on many services. So one leak also opens their accounts on other services. That is why passwords are never stored as they are. What is stored is their hash.
Hash: a one-way fingerprint
A hash turns any text into a run of characters with a fixed length. The process is one-way: going from password to hash is easy, but going back from hash to password is (nearly) impossible. At login, the server hashes the password you typed, then compares it with the stored hash. If they match, the password is correct.
Why a fast hash is wrong
Hashes like MD5, SHA-1, and SHA-256 are built to be fast. For files, that speed is good. For passwords, that speed is a disaster. In the Lab you can hash a password with SHA-256. This is the SHA-256 hash of secret123:
SHA-256("secret123")
= fcf730b6d95236ecd3c9fc2d92d7b6b2bb061514961aec041d6c7a7192f592e4
Before you read on, guess: two people happen to use the same password, password123. Are their SHA-256 hashes the same or different?
The answer: the same. The exact same hash comes out every time, because SHA-256 is deterministic: the same input always gives the same output. Three problems come from that:
- The same password gives the same hash.
password123for two people gives an identical hash (ef92b778...). An attacker who sees two equal hashes knows right away that those two people use the same password. - Rainbow table. Because the result never changes, an attacker can prepare a huge table in advance, with millions of
password -> hashpairs. With a stolen hash, the attacker only has to look it up in the table, and the password is found right away. - Lightning brute force. Brute force means trying guesses one by one until one matches. SHA-256 is very fast: one modern GPU (graphics card) can try more than ten billion guesses per second. A short password is found in seconds.
The conclusion: do not use MD5, SHA-1, or SHA-256 for passwords.
Salt: make each hash unique
A salt is a random string joined to the password before hashing. Each user gets a different salt. The result:
password123+ salt A gives a hash different frompassword123+ salt B. So two people with the same password now have different hashes.- The rainbow table is useless. It was prepared without the salt, so what it holds no longer matches a salted hash.
A salt is not secret. It is stored next to the hash. Its purpose is not to hide anything, but to make each hash unique.
bcrypt and argon2id: deliberately slow
bcrypt and argon2id are hash algorithms designed for passwords. Both have two properties SHA-256 lacks:
- Automatic salt. The salt is made and stored inside the hash itself, so you do not manage it separately.
- Cost factor. The cost factor is a number that sets how slow the hashing is. In bcrypt, each step up doubles the work required. For you, the slowness is only milliseconds per login. For an attacker who must try billions of guesses, it is a huge slowdown.
The hash looks like this (notice the format, not the value):
bcrypt : $2b$12$R9h/cIPz0gi.URNNX3kh2OPST9/PgBkqquzi.Ss7KIUgO2t0jWMUW
argon2id : $argon2id$v=19$m=19456,t=2,p=1$c29tZXNhbHQ$RdescudvJCsgt3ub...
$2b$marks bcrypt,12is the cost factor, and the rest is the salt and the hash joined together.$argon2id$marks argon2id.m=19456is the memory it uses (about 19 MiB),t=2the number of iterations (passes of the process), andp=1the parallelism (how many lanes work at the same time).
OWASP's current recommendation: use argon2id (minimum 19 MiB memory, 2 iterations, 1 parallelism), or, on older systems that already use bcrypt, a cost of at least 10. OWASP also says one hashing should take less than one second. Within that limit, set the cost so one hashing on your server takes about a quarter of a second.
In PHP: password_hash and password_verify
PHP already has two safe functions for this, and you use both in the exercise:
$password = "secret123";
$hash = password_hash($password, PASSWORD_DEFAULT); // bcrypt, automatic salt
echo "Hash created\n";
// Verify: match the raw password against the stored hash
echo "Password matches: " . (password_verify("secret123", $hash) ? "true" : "false") . "\n";
echo "Wrong password: " . (password_verify("wrong_password", $hash) ? "true" : "false") . "\n";
password_hash($password, PASSWORD_DEFAULT)makes a hash with the best available algorithm. Right nowPASSWORD_DEFAULTmeans bcrypt.- A bcrypt hash from PHP starts with
$2y$12$, not$2b$as in the sample above.$2y$is PHP's marker for bcrypt, and12is the default cost since PHP 8.4. password_verify($input, $hash)matches a raw password (the typed password, not hashed yet) against the hash. Never compare two hashes with an equals sign. A password hashed again gets a new salt, so the result is never identical to the stored hash, even when the password is the same.- The output:
Hash created, thenPassword matches: true, thenWrong password: false.
⚠️ Storage choices that make a leak worse
- Using MD5 or SHA for passwords. Too fast, no salt, and many rainbow tables for them already exist. Use
password_hash(). - Comparing hashes yourself.
if (md5($input) === $stored)is wrong twice: the algorithm is wrong, and the check should usepassword_verify(). - Keeping a secret salt apart and treating it as a key. A salt is not secret, and it is already stored inside the bcrypt/argon2 hash. What makes the hash slow is the cost factor, not a hidden salt.
- Limiting password length too tightly. Allow long passwords (at least 8, up to 64 characters or more), and do not cut them in the middle.
- Forgetting that bcrypt reads only the first 72 bytes. The rest is ignored without any message. A plain Latin letter takes 1 byte, but emoji and non-Latin letters take several bytes per character (🔒 is 4 bytes). So you reach the limit sooner than you might expect. If you allow very long passwords, use
PASSWORD_ARGON2ID, which has no such limit. The code below hashes 72aletters withXYZadded, then checks only the 72aletters against that hash:
$pw = str_repeat("a", 72);
$bcrypt = password_hash($pw . "XYZ", PASSWORD_DEFAULT);
var_dump(password_verify($pw, $bcrypt)); // bool(true): "XYZ" is ignored
$argon = password_hash($pw . "XYZ", PASSWORD_ARGON2ID);
var_dump(password_verify($pw, $argon)); // bool(false)
TL;DR: Passwords are stored as a hash (a one-way fingerprint that can't be turned back into the password), not as plain text (text as it is). MD5/SHA-256 are wrong for passwords for two reasons. First, they are too fast: one GPU (graphics card) can try more than ten billion password guesses per second. Second, they are deterministic: the same password always gives the same hash, so rainbow tables (lists of password and hash pairs prepared in advance) work. A salt (a random string per user, joined to the password) makes each hash unique. bcrypt and argon2id are deliberately slow through a cost factor (a number that sets the slowness) and carry an automatic salt. In PHP, make a hash with password_hash($pw, PASSWORD_DEFAULT) (bcrypt), and match it with password_verify($input, $hash). OWASP now recommends argon2id (19 MiB memory, 2 passes, 1 lane), or bcrypt with a cost of at least 10 on older systems.
⚡ Store the fingerprint, not the password
Meaning: A password is never stored as it is (plain text). What's stored is its hash: a one-way fingerprint. Going from password to hash is easy, but going back from hash to password is (nearly) impossible. At login, the server hashes the typed password, then compares it with the stored hash.
⚡ A fast hash is wrong
Guess first: two people both use password123. SHA-256 is one hash algorithm. Are their SHA-256 hashes the same or different?
Exactly the same. SHA-256 is deterministic: same input, same output. That's why a rainbow table (a list of password and hash pairs prepared in advance) finds the password right away.
Meaning: MD5, SHA-1, and SHA-256 are built to be fast: one GPU (graphics card) can try more than ten billion password guesses per second. Don't use them for passwords.
⚡ Salt and cost factor
Meaning: A salt is a random string joined to the password before hashing, so each hash is unique even when the passwords are the same. bcrypt and argon2id are hashes made for passwords: both carry an automatic salt, and both are deliberately slow through a cost factor (a number that sets the slowness). In bcrypt, each step up in cost doubles the work.
⚡ In PHP
Write:
$hash = password_hash($password, PASSWORD_DEFAULT); // bcrypt, automatic salt
echo "Password matches: " . (password_verify("secret123", $hash) ? "true" : "false") . "\n";
Meaning: password_hash makes a bcrypt hash of $password. password_verify matches a raw password (the typed one, not hashed yet) against the stored hash. If $password holds secret123, the second line prints Password matches: true. Never compare two hashes with an equals sign: a password hashed again gets a new salt, so the result is always different.