Storing passwords safely

Hash passwords with a salt using Node's built-in scrypt — and why encryption is the wrong tool.

Module 6: Authentication and security · Lesson 19 of 26 · 17 min read · Free

Databases get stolen — through bugs, careless backups, or insiders. When that happens, the passwords inside must be useless to the thief. People reuse passwords, so a leak from your app could empty someone's bank account.

Hash, don't encrypt

  • Encryption can be reversed with a key. If the key is stolen too, every password is exposed.
  • A hash is one-way: you can check a password against it, but not turn it back into the password.

Not every hash is good for passwords. Fast hashes like SHA-256 let attackers try billions of guesses a second. Use a deliberately slow password hash: scrypt (built into Node), bcrypt or argon2.

Salt

If two users both choose Password123, plain hashes would be identical — and attackers have huge pre-computed tables of common passwords. A random salt per user makes every hash unique.

Hashing a password with a salt
import { scryptSync, randomBytes, timingSafeEqual } from "node:crypto";

export function hashPassword(password) {
  const salt = randomBytes(16).toString("hex");
  const hash = scryptSync(password, salt, 64).toString("hex");
  return `${salt}:${hash}`;                 // store both together
}

export function verifyPassword(password, stored) {
  const [salt, hash] = String(stored).split(":");
  if (!salt || !hash) return false;
  const test = scryptSync(password, salt, 64);
  const known = Buffer.from(hash, "hex");
  return known.length === test.length && timingSafeEqual(known, test);
}

timingSafeEqual compares in constant time, so attackers can't learn anything from how long the comparison takes.

Password rules that help

  • A minimum length (8+, ideally 10+) matters more than complex character rules.
  • Allow long passphrases and paste (password managers).
  • Never send passwords back in responses, never log them, never email them.
  • Offer password reset through a time-limited random token sent by email or SMS — never “here's your password”.

Check yourself

Answer in your head first, then open each question to see the answer and why.

  1. Why add a random salt to each password before hashing?

    • A So identical passwords produce different hashes and pre-computed tables don't work
    • B To make passwords shorter
    • C To encrypt them
    • D To speed up login
    Show the answer

    A) So identical passwords produce different hashes and pre-computed tables don't work — Salts make every hash unique.

  2. Which is suitable for hashing passwords?

    • A scrypt, bcrypt or argon2
    • B Plain SHA-256
    • C Base64 encoding
    • D Reversible encryption
    Show the answer

    A) scrypt, bcrypt or argon2 — Password hashes are deliberately slow.

  3. A user forgets their password. What should your app do?

    • A Send a time-limited reset link so they can choose a new one
    • B Email them their password
    • C Read the password from the database
    • D Reset it to "123456"
    Show the answer

    A) Send a time-limited reset link so they can choose a new one — You can't — and shouldn't be able to — recover the original.

More free lessons: What the back end does · Install Node.js and use the terminal · Asynchronous code: promises and async/await · HTTP: requests, responses and status codes · Designing a REST API · Designing tables and relationships · Automated tests and project structure