Skip to main content
1:1 mock interviews with Sumit, scored on a real hiring rubric.Book a slot →
TOPIC 3 OF 5 · 2 MIN READ

Storing Passwords Safely

Passwords must never be stored in plain text or with fast hashes like MD5 or SHA-256. Store them with a slow, salted password hash such as Argon2id or bcrypt, so a leaked database does not become a list of passwords.

What you will learn

  • Explain hashing vs encryption
  • Explain salts, slow hashes and pepper
  • Choose Argon2id or bcrypt and tune its cost
  • Design a safe reset-password flow

Key concepts

Hash, not encrypt

Encryption can be reversed with a key; hashing is one-way. You never need the original password, only to check a guess, so hash.

Salt

A random value per user stored with the hash. Same passwords get different hashes, which defeats precomputed rainbow tables.

Slow hash

Argon2id (memory-hard, current recommendation), scrypt or bcrypt take tens to hundreds of milliseconds on purpose, so guessing billions of passwords becomes impractical.

Timing-safe compare

Compare secrets with crypto.timingSafeEqual so response time does not reveal how many characters matched.

Step by step

Reset password flow

  • Always reply "if the email exists, we sent a link", to avoid account enumeration
  • Create a random token, store only its hash with a 15 to 60 minute expiry
  • Email the link; on use, verify the hash and expiry, set the new password, delete the token
  • Invalidate the user's existing sessions

Examples

Argon2id in Node.js

import argon2 from 'argon2'

const hash = await argon2.hash(password, { type: argon2.argon2id })
// $argon2id$v=19$m=65536,t=3,p=4$<salt>$<hash>  ← salt and settings are stored in the string

const ok = await argon2.verify(user.passwordHash, attempt)

Practise it

Try these without looking back. If you can explain each one out loud, you are ready for this question.

  • Explain why SHA-256 with a salt is still not enough.
  • Add login rate limiting per account and per IP.

Interview questions

What interviewers ask next on this topic. Answer out loud first, then open the model answer.

What is a pepper?

A secret added to every password before hashing, stored outside the database (environment or a secrets manager). If only the database leaks, attackers cannot even start guessing without it.

How do you upgrade from bcrypt to Argon2id?

On the next successful login you have the plain password, so verify with bcrypt, re-hash with Argon2id and save. Store the algorithm with each hash so both can coexist.

Summary

Hash passwords with Argon2id or bcrypt (salted and slow), compare in constant time, avoid account enumeration, and store only hashes of reset tokens.

Stuck on this one? Talk to Sumit.

Keys: F focus mode · ← → previous / next topic · Esc leave focus