FresherTechnicalAuthentication & security
How should a backend store user passwords?
Try answering first
Model answer
Never store the password itself. Store the output of a slow, salted password hash, so a stolen database does not hand attackers the passwords.
Why the simpler options fail:
- Plain text or reversible encryption: one leak or one stolen key exposes everyone.
- Fast hashes such as MD5 or SHA-256: a GPU tries billions of guesses per second, and precomputed rainbow tables work when there is no salt.
What to do instead:
- Use argon2id, or bcrypt or scrypt where argon2 is unavailable. These are deliberately slow and memory-hungry, with a cost setting you raise over time.
- Use a unique random salt per password, so identical passwords produce different hashes. Modern libraries generate it and embed it in the hash string.
- Verify with the library's compare function, which runs in constant time.
- Let users choose long passphrases, check new passwords against breach lists, and skip arbitrary composition rules.
- Add MFA, because a good hash still cannot save a weak or reused password.
Takeaway: use a maintained library, let it handle salt and parameters, and never invent your own scheme.
Key points
- Store a slow salted hash, never the password
- Prefer argon2id, with bcrypt or scrypt as alternatives
- A unique salt per password defeats rainbow tables
- Use a library and add MFA
Likely follow-ups
- What is the difference between a salt and a pepper?
- How do you increase the cost parameters later?
Common mistakes
- Using SHA-256 or MD5 for passwords
- Using one global salt for all users