How passwords are really stored

A well-built website never keeps your password. Here is what it keeps instead, why "hashed" does not mean "safe", and what that means for the passwords you pick.

beginnerdraft6 min readPublished

When you sign up to a website, you hand over a secret and trust the site to look after it. Here is the surprising part: a well-built site never keeps your password at all. It keeps something else, and the difference decides how much damage a stolen database can do.

The short version

  • A site should store a salted, slow hash of your password, not the password itself.
  • A hash is a one-way fingerprint. The site can check a password you type, but cannot read the original back out.
  • "Slow" matters. Fast hashes such as plain SHA-256 are the wrong tool for passwords.
  • You cannot see how a site stores your password, so protect yourself: one unique password per site, long, ideally from a password manager.

Why not just store the password?

Imagine the database leaks. It happens to organisations of every size, through a bug, a stolen backup or a careless admin. If the passwords were stored as plain text, the attacker now has a working key for every account. Worse, many people reuse passwords, so the damage spreads to their email, banking and work accounts too.

Even without a leak, plain-text storage means that staff with database access can read everyone's passwords. A site that can read your password is a site that can lose it.

The first idea: a hash

A hash function takes any input and produces a fixed-size output, like a fingerprint. Two properties matter here. It is one-way, so you cannot run it backwards. And a tiny change to the input gives a completely different output:

console
printf 'password1' | sha256sum
0b14d501a594442a01c6859541bcb3e8164d183d32937b851835442f69d5c94e  -
printf 'password2' | sha256sum
6cf615d5bcaac778352a8f1f3360d23f02f34ec182e259897fd6ce485d7870d4  -

When you register, the site hashes your password and stores only the result. When you log in, it hashes what you typed and compares the two fingerprints. If they match, you knew the password. The original never needs to be stored.

The catch: fast hashes are too fast

SHA-256 is brilliant for checking that a file has not changed, partly because it is quick. That same speed is a flaw for passwords.

If an attacker steals the database, they can guess passwords offline, on their own hardware, with no rate limits and no one watching. They hash one guess after another, starting with common passwords and predictable variations, and compare each result with the stolen fingerprints. The faster the hash, the more guesses per second they get. Short or common passwords fall almost immediately.

Salt: stop identical passwords looking identical

Two users who pick the same password would get the same hash. Worse, an attacker could prepare a giant table of hashes for common passwords in advance and look the stolen ones up.

A salt fixes both problems. It is a random value, unique to each user, mixed into the password before hashing. It is stored right next to the hash and is not secret. Its job is to make every stored value different, so precomputed tables are useless and each password has to be attacked individually.

Slow on purpose

The real fix is to use a function designed for passwords, one that is deliberately expensive to compute. The well-known choices are:

FunctionWhat makes it expensive
Argon2idNeeds a configurable amount of memory and time
scryptAlso memory-hungry, which hurts attackers' specialised hardware
bcryptAn older design with an adjustable "cost" setting
PBKDF2Repeats a hash many times, with an adjustable iteration count

The OWASP cheat sheet lists Argon2id as its first choice, and gives current minimum settings for each of the others. Those numbers change over time, so look them up rather than trusting an article, including this one.

Here is what the idea looks like in Python, using the scrypt function from the standard library. It is a teaching example, not something to paste into a real login system:

hash_password.py
import hashlib, hmac, os

def hash_password(password: str) -> str:
    salt = os.urandom(16)                       # random, different for every user
    key = hashlib.scrypt(password.encode(), salt=salt, n=2**14, r=8, p=1, dklen=32)
    return salt.hex() + "$" + key.hex()         # store both: the salt is not secret

def check_password(password: str, stored: str) -> bool:
    salt_hex, key_hex = stored.split("$")
    key = hashlib.scrypt(password.encode(), salt=bytes.fromhex(salt_hex), n=2**14, r=8, p=1, dklen=32)
    return hmac.compare_digest(key, bytes.fromhex(key_hex))   # constant-time comparison

stored = hash_password("correct horse")
print(check_password("correct horse", stored))   # True
print(check_password("wrong guess", stored))     # False

Some systems also add a pepper: a secret value kept outside the database, for example in a hardware module or secrets manager, that is mixed in alongside the salt. It is an optional extra layer. If the database leaks but the pepper does not, the attacker is missing a key ingredient.

What this means for you

You cannot inspect how a website stores your password, but you can limit the harm if it does it badly.

  • Use a different password for every site. Reuse is what turns one leak into many break-ins.
  • Make them long. The NCSC recommends three random words as a memorable way to build a strong password, and a password manager as a way to avoid having to remember most of them.
  • Turn on two-step verification where it is offered, so that a stolen password alone is not enough.
  • Check for known leaks. Have I Been Pwned lets you see whether your email address appears in known breaches. Its Pwned Passwords service is built so that a password can be checked without sending the whole thing: only the first five characters of the password's SHA-1 hash leave your device.

A warning sign worth knowing: if a site's "forgot password" feature emails you your existing password, it is storing it in a recoverable form. A properly hashed password cannot be sent back to you, which is why good sites send a reset link instead.

NIST's digital identity guidelines also point away from old habits such as forcing regular password changes and demanding odd mixtures of symbols. Length and uniqueness do more good.

What hashing does not protect

A good hash makes each guess expensive, but it does not turn a weak password into a strong one. If your password is "Summer2026!", it will be among the first guesses however carefully it was stored. Slow hashing buys time. Long, unique passwords make that time count.

Where to go next

If you want to see the same ideas from the attacker's side of the maths, the next step is how encryption keys are agreed between strangers, which is exactly what the TLS walkthrough covers.

Sources

  1. OWASP Password Storage Cheat Sheet cheatsheetseries.owasp.org
  2. RFC 9106, Argon2 Memory-Hard Function for Password Hashing www.rfc-editor.org
  3. RFC 7914, The scrypt Password-Based Key Derivation Function www.rfc-editor.org
  4. RFC 8018, PKCS #5 Password-Based Cryptography Specification (PBKDF2) www.rfc-editor.org
  5. Provos and Mazières, A Future-Adaptable Password Scheme (bcrypt), USENIX 1999 www.usenix.org
  6. NIST SP 800-63B, Digital Identity Guidelines: Authentication and Authenticator Management pages.nist.gov
  7. NCSC, Use three random words www.ncsc.gov.uk
  8. NCSC, Using password managers www.ncsc.gov.uk
  9. Have I Been Pwned, API v3 (Pwned Passwords range search) haveibeenpwned.com