SDSystem DesignConcepts to case studies
System DesignDSA
x
0/311
Part 1: Foundations
Part 2: APIs and Communication
Part 3: Caching
Part 4: Data and Storage
Part 5: Architecture
Part 6: Distributed Systems
Part 7: Advanced Data Topics
Part 8: Operations and Security
  • SSL/TLS
  • Encryption at Rest
  • Secrets Management
  • Password Management
  • RBAC
  • SAML
  • OWASP Top 10 and Common Attacks
  • DDoS Attacks and Defenses
  • Web Application Firewall (WAF)
  • Certificates and PKI
  • Multi-Factor Authentication (MFA)
  • Zero Trust
  • Key Management (KMS)
  • Data Privacy and GDPR Basics
Part 9: Estimation and Interview Method
Part 10: Case Studies
System Design/Operations and Security/Advanced Security

Password Management

Store user passwords safely with salting and slow hashes, and handle resets well.

⏱ 14 min readIntermediateMust KnowInterview FavouriteHas visualizer
SummaryConceptReal-Life ExampleWhy?Where?How It WorksStepsKey TermsPros & ConsMistakesReal WorldRead NextInterview Q&ACheat-Sheet

01Quick Summary

Password management is how a system stores and checks its users' passwords so that a stolen database does not hand attackers the passwords.

  • Never store the password itself, and never use plain encryption. Store a one-way hash made by a slow, salted algorithm such as Argon2id, bcrypt or scrypt.
  • Each user gets a unique random salt, so equal passwords do not look equal and precomputed tables do not work.
  • Add rate limits, breach checks, multi-factor sign-in and a safe reset flow. A hash cannot be "decrypted", it can only be guessed against.

02Concept: What is it?

When a user signs up, the server must later be able to tell whether a typed password is right. It does not need to know the password to do that. It only needs to be able to check it. A hash function makes this possible: it turns the password into a fixed-size value, and you cannot run it backwards. At login, the server hashes what the user typed and compares the result with the stored value.

But not every hash is good for passwords. General-purpose hashes (like plain SHA-256 or MD5) are built to be fast, and fast is exactly what an attacker wants: a stolen table can be attacked with a huge number of guesses per second. Password hashes such as Argon2id, bcrypt and scrypt are built to be slow (and some use a lot of memory), with a setting you can raise over time. That multiplies the cost of every guess.

A salt is a random value, unique per user, stored next to the hash and mixed into it. Two users with the same password end up with different hashes, and an attacker cannot reuse one precomputed table of common passwords against everyone. An optional pepper is a further secret kept outside the database (for example in a secret manager), so a stolen database alone is not enough to start guessing.

Sign up and sign in
⌨️Passwordtyped by the user
→↓+ unique salt
🐢Slow hashArgon2id, bcrypt, scrypt
→↓store
🗄️Databasesalt + hash only
The server never stores the password. It stores the salt and the slow hash, and repeats the same steps at login.
🧠Hashing is not encryption
Encryption can be reversed with a key. A password hash cannot be reversed at all. Attackers do not "decrypt" it. They guess passwords, hash each guess the same way and compare. That is why the speed of the hash decides how safe the passwords are.

03Real-Life Example

🍵The Fingerprint Card Instead of the Key
A club wants to know that only members come in, but it does not want to keep a copy of every member's house key. So at sign-up it keeps a small card made by a special stamping machine from the member's secret phrase. The stamping machine is slow on purpose and adds a different random mark for each member. At the door, the member says their phrase, the club stamps it the same way and checks that it matches the card. If a burglar steals the box of cards, they cannot read any phrase. They have to guess phrases and stamp each one slowly, one by one.
The club
Password storage
The member's secret phrase
The user's password
A card made by the stamping machine
The stored hash
The machine is slow on purpose
A slow hash such as Argon2id or bcrypt
A different random mark per member
A unique salt per user
A secret mark kept in a safe, not in the box
A pepper kept outside the database
The burglar guessing phrases one by one
An offline guessing attack on a stolen table
A limit on tries at the door
Rate limiting and lockouts at login
Changing the stamp setting to be slower
Raising the cost factor over time
Checking a login
⌨️User typespassword
→↓
🧂Load saltfor that user
→↓
🐢Hash againsame settings
→↓
⚖️Comparewith stored hash

04Why Do We Need It?

🗄️Databases do leak
Plan for the day the user table is stolen. Good storage means the thief gets very little.
🔁People reuse passwords
A leak from your site can open the same users' email or bank accounts elsewhere.
🐢Slow means safer
A deliberately costly hash makes every guess expensive for the attacker.
🧂Salts break shortcuts
Unique salts defeat precomputed tables and hide which users share a password.
🚪Stop guessing online
Rate limits, lockouts and extra factors block attackers who try your login form.
⚖️Legal duty
Many privacy and security rules expect passwords to be protected with strong, standard methods.
Store a slow, salted hash, never the password. Then a stolen table is a long guessing job, not a gift.

05Where Do We Use It?

🔑Sign-up and login
Every system with its own accounts hashes at sign-up and verifies at login.
📧Password reset
A reset sends a single-use, expiring link, and sets a new hash when used.
🔧Password change
Ask for the old password, then hash and store the new one with the current settings.
⬆️Upgrading old hashes
When a user logs in, re-hash with a stronger setting and replace the old value.
🤝Using an identity provider
With Single Sign-On or OAuth login, someone else stores the passwords, and you store none.
🧑‍💻Password managers
Users keep long, unique passwords in a manager, so each site is not a shared risk.

06Types / Parts / How It Works

Ways to store a password (best at the bottom)

MethodWhat an attacker with the table can doVerdict
Plain textRead every password at onceNever
Reversible encryptionDecrypt every password if they also get the key, which lives close byNever. The system should not be able to recover passwords.
Fast hash, no salt (plain SHA-256, MD5)Look up common passwords in precomputed tables, spot shared passwords, and guess very fastNot acceptable
Fast hash + saltCannot use shared tables, but can still guess very fastNot good enough for passwords
Slow salted hash: Argon2id, bcrypt, scryptMust guess one password at a time, and each guess is costlyUse this
Slow salted hash + pepper in a secret managerCannot even start guessing without the pepperBest, with more moving parts

The three standard slow hashes

AlgorithmWhat makes it slowNotes
Argon2idTime and a lot of memory, which resists special hardwareA modern first choice for new systems
scryptTime and memoryWell respected, and a good option if Argon2id is not available
bcryptTime (a cost factor)Widely supported and still acceptable, with a limit on very long passwords in many versions
📝Note
Settings (cost, memory, parallelism) should follow the current recommendations of a trusted security guide, and be raised over time. Tune them so a login takes a noticeable but acceptable fraction of a second on your servers.
VisualizerSteal the table: what does the attacker see, and how long does guessing take?Start with plain text, then switch through fast hash, salted hash and slow salted hash. Notice Alice and Bob's values, then change password length and characters and watch the time to guess.
How are passwords stored?

The password itself is stored. Anyone who reads the table reads every password.

Toy hash for teaching only. It is not real cryptography.

userstored in the database (stolen)
alicesunshine
bobsunshine
carolviolet-lamp-river-stone
Alice and Bob both picked the same password. Their stored values match, so an attacker can see they are equal.
The thief reads every password at once. Many people reuse passwords, so the damage spreads to other sites. Never store the password itself.
Characters used
Time to guess (avg)
no guess needed
for a random password of this kind
Guesses per second
n/a
illustrative attacker
Possible passwords
2.1e+11
26 symbols, length 8

Guess speeds are made-up illustrative numbers for a well-equipped attacker, to show the shape and not to predict real attacks. Real people pick predictable passwords (names, words, patterns), which are guessed far sooner than random ones. That is why length, breach checks and slow hashes all matter.

07How to Use It / Steps / Implementation

Doing it right

  1. 1
    Use a proven library
    Call the password-hashing function of a well-reviewed library for Argon2id, scrypt or bcrypt. Never write your own hash or mix your own scheme.
  2. 2
    Let the library make the salt
    A unique random salt per password, stored together with the hash. Most libraries do this and put everything into one string.
  3. 3
    Choose a good cost
    Follow the current guidance, and test on your own hardware. Plan to raise it as hardware gets faster, and re-hash users at their next login.
  4. 4
    Compare safely
    Use the library's verify function, which compares in a way that does not leak timing. Do not compare hashes with a plain string check.
  5. 5
    Check new passwords
    Allow long passphrases, avoid pointless composition rules, and reject passwords found in known breach lists or that are very common.
  6. 6
    Limit and watch logins
    Rate-limit by account and by address, slow down repeated failures, alert on unusual patterns and offer multi-factor sign-in.
  7. 7
    Make reset safe
    Send a random, single-use token that expires soon and is stored hashed. Do not reveal whether an email exists, and end old sessions after a reset.
  8. 8
    Never log or email passwords
    Do not write passwords to logs, error reports or analytics, and never send one back to the user. If you can email a user their password, you stored it badly.
💡Consider not storing passwords at all
Sign in with an identity provider (SSO, OAuth), or use passkeys. Then someone else carries the risk, and users have fewer passwords to lose.
⚠️Do not roll your own
Home-made schemes, such as hashing twice with a fast hash or adding a secret "trick", are almost always weaker than they look. Use the standard algorithms.

08Key Terms to Know

TermMeaning in easy words
Hash functionA one-way function that turns input into a fixed-size value that cannot be reversed
Password hashA deliberately slow hash made for storing passwords, such as Argon2id, bcrypt or scrypt
SaltA unique random value per user, mixed into the hash and stored beside it
PepperA secret value kept outside the database and mixed into every hash
Cost factorA setting that controls how slow (and costly to attack) the hash is
Rainbow tableA precomputed list of hashes for common passwords. Unique salts make it useless
Brute forceTrying many passwords one after another until one works
Credential stuffingTrying passwords leaked from other sites against your login
Rate limitingSlowing or blocking repeated attempts from one account or address
MFAMulti-factor authentication: a second proof besides the password
PasskeyA passwordless sign-in method based on public-key cryptography

09Pros, Cons and Trade-offs

✅ Pros
  • A stolen table does not reveal passwords directly
  • Slow hashing makes each guess costly
  • Unique salts hide shared passwords and stop precomputed attacks
  • Standard libraries make it easy to do correctly
  • Cost can be raised as hardware improves
⚠️ Cons
  • Hashing uses real CPU and memory, which can strain login servers
  • Weak user passwords can still be guessed
  • A high cost can be abused to overload a login endpoint
  • Passwords cannot be recovered, only reset
  • Migrating old hashes needs care

Trade-offs

ChoiceWe gainWe lose
Higher cost factorEach guess costs the attacker moreSlower logins and more server load
Lower cost factorFast logins, cheap serversCheaper guessing for an attacker
Adding a pepperA stolen database is not enough to guessA secret to manage, and rotating it is harder
Strict composition rulesLooks safeUsers pick predictable patterns. Length and breach checks work better
Account lockoutStops online guessingAttackers can lock real users out on purpose. Prefer rate limits and delays
Login with an identity providerNo passwords to storeA dependency, and less control over the sign-in experience

10Common Problems and Mistakes

Common mistakes

  1. Storing plain text or encrypting reversibly. A leak or a stolen key exposes every password.
  2. Using a fast hash. Plain MD5 or SHA-256, even with a salt, lets attackers test a huge number of guesses per second.
  3. One salt for everyone, or no salt. Equal passwords look equal, and one table attacks all users.
  4. Writing your own scheme. Home-made constructions break in ways you cannot see.
  5. Leaking through resets. Long-lived or reusable reset links, or messages that reveal which emails exist.
  6. No limit on login tries. The login form becomes a free guessing machine.
  7. Logging or emailing passwords. Debug logs and welcome emails copy them to places that were never meant to hold them.

Things to watch

🥵Login as a load attack
An expensive hash can be used to overload your servers. Rate-limit before hashing.
🔁Old hashes
Users who never log in keep old, weaker hashes. Re-hash at login and consider forcing a reset for very old ones.
🕵️Credential stuffing
Attacks using other sites' leaks look like normal logins. Use breach checks, MFA and anomaly alerts.
📏Length limits
Do not cap passwords at a short length. Allow long passphrases and all characters.

11Real-World Examples

🏦Banks and large sites
Add MFA and risk checks on top of strong password storage, so a stolen password alone is not enough.
🧰Web frameworks
Popular frameworks include a password-hashing helper with a sensible default. Use it instead of building your own.
🔑Password managers
They let users keep long, unique passwords per site, which limits the damage of any one leak.
🪪Passkeys and SSO
More sites let users sign in without a password at all, or through a company or social identity provider.
📣Breach notifications
After a leak, good sites force resets, invalidate sessions and tell users, which is easier when hashes were strong.
🔎Breach checking
Services let a site check whether a candidate password appears in known leaks, without sending the password itself.
📝Note
Recommended algorithms and settings change as hardware and research move on. Check a current, trusted security guide when you pick them.
Mini case: the user table is stolen
Fast hash, no salt
🗄️Stolen table
→↓lookup + fast guessing
🔓Most passwordscracked soon
Slow salted hash
🗄️Stolen table
→↓slow one-by-one guessing
🛡️Only weak onesfall, slowly
Fast to crack Slow to crack
The same breach, with different storage.

12Related Topics to Read Next

Secrets Management→

A pepper and other keys belong in a secret manager.

Encryption at Rest→

Encryption is reversible, which is why it is wrong for passwords.

Authentication vs Authorization→

Passwords prove who you are. This is the first half.

Session vs Token Based Auth→

What happens after a successful login.

Rate Limiting→

Stops online password guessing against the login form.

Single Sign-On (SSO)→

Lets you avoid storing passwords yourself.

13Interview Questions with Short Answers

14Quick Revision Cheat-Sheet

  • ✔Never: plain text, reversible encryption, or fast hashes like plain MD5 or SHA-256.
  • ✔Use: Argon2id, scrypt or bcrypt through a proven library.
  • ✔Salt: a unique random value per user, stored with the hash.
  • ✔Pepper: optional secret kept outside the database, in a secret manager.
  • ✔Cost: tune on your hardware and raise over time. Re-hash at login.
  • ✔Login: rate-limit, check breached passwords, offer MFA, allow long passphrases.
  • ✔Reset: single-use, expiring, hashed token. Same reply whether or not the email exists.
  • ✔One line: “Keep a slow, salted fingerprint of the password, never the password.”
← Previous
Secrets Management
Next →
RBAC