Backend & Node.js · APIs, data and failure
How should you store passwords securely?
Short answer
Store a salted, slow password hash from bcrypt, scrypt or Argon2id. Never store plaintext or reversible encryption, and tune the cost so one hash takes a deliberate fraction of a second.
Passwords need a one-way function that is expensive to brute force. A general-purpose hash such as SHA-256 is too fast; attackers can test billions of candidates per second. Password hashing algorithms add a salt and a configurable work factor.
import { hash, verify } from "@node-rs/argon2";
const digest = await hash(password, {
memoryCost: 19456,
timeCost: 2,
parallelism: 1,
});
const valid = await verify(digest, password);The rules
- Use a unique random salt per password, generated by the library
- Use Argon2id, scrypt or bcrypt; never MD5, SHA-1 or plain SHA-256
- Tune cost against your hardware and measure login latency
- Compare with a constant-time verify function from the library
- Rehash on successful login when you raise the cost parameters
- Rate limit login attempts and add breached-password checks
bcrypt has a 72-byte input limit and truncates longer passwords silently or throws depending on the implementation. Argon2id is the modern default and lets you tune memory separately from CPU. A pepper, stored outside the database, adds defense in depth but complicates rotation.
Follow-ups you should expect
Interviewers use these to check whether the first answer was understanding or memorisation.
- 1Why is SHA-256 unsuitable for passwords?
- 2What is the difference between a salt and a pepper?
- 3Why does bcrypt have a 72-byte limit?
- 4When should you rehash a password?
Say it in the interview
I store passwords with a slow, salted hash such as Argon2id or bcrypt, never with plaintext or reversible encryption. The algorithm's cost is tuned so verification takes a deliberate fraction of a second, and I rehash on login when raising parameters. I also rate limit attempts and check for breached passwords. General-purpose hashes are too fast to resist offline cracking.
Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.
Related questions
Reading is step one
Now say it out loud.
Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live Backend & Node.js mock interview and get scored feedback on depth, clarity and confidence.