Learn to Code
All tracks
Learn to Code

Free lessons from scratch to pro — every language, databases and DevOps.

AboutContactPrivacy PolicyTerms of Use

Your progress is saved in this browser. © 2026 Learn to Code

SEC
Authentication & Security
0/10 completed
Beginner
Authentication vs authorizationHashing passwords
Intermediate
Sessions and cookiesJSON Web TokensOAuth 2.0 and OpenID ConnectMulti-factor authentication and passkeys
Advanced
The OWASP Top 10XSS, CSRF and CORSSecrets management and encryption
Pro
Security in practice
Tracks / Authentication & Security / Beginner

Hashing passwords

Databases get stolen: through SQL injection, leaked backups, misconfigured servers or malicious insiders. When it happens, what you stored decides whether your users lose one account or every account where they reused that password. This lesson shows why plain text and fast hashes fail, and how to store passwords correctly.

What not to do

Storage What an attacker with the database gets
Plain text Every password, instantly
Encrypted Every password, as soon as they find the key (which lives on the same server)
Fast hash (MD5, SHA-256) Most passwords within hours
Slow, salted password hash (Argon2id, scrypt, bcrypt) Only the very weakest passwords, slowly and expensively

A hash function turns any input into a fixed-size fingerprint, and it can't be reversed. So why aren't ordinary hashes enough?

Problem 1: identical passwords, identical hashes

Node.js · editable

Asha and Priya have the same hash, so cracking one cracks both. Worse, attackers keep huge precomputed tables of hashes of common passwords (rainbow tables): a lookup instantly reveals sunshine1.

The fix is a salt: a random value, unique per user, mixed into the hash and stored next to it. Same password, different hashes, and precomputed tables become useless.

Problem 2: fast hashes are too fast

SHA-256 is designed to be fast. A single modern graphics card computes billions of SHA-256 hashes per second, so even salted, an attacker can try every common password for each user:

Node.js · editable

Password hashing functions are deliberately slow and memory-hungry, with a tunable cost. Taking 100 ms per hash is unnoticeable when a user logs in, but turns billions of guesses per second into a handful.

Doing it right: scrypt (built into Node)

Node's crypto module includes scrypt, a memory-hard password hash recommended by OWASP:

Node.js · editable

The stored string records the algorithm and parameters with the salt and the hash. That lets you raise the cost later: when a user logs in successfully with an old-parameter hash, re-hash their password with the new settings.

bcrypt

bcrypt (1999) is still very widely used and is fine to keep using. Its string format packs everything in, so its API is tiny:

Node.js · editable

bcrypt only uses the first 72 bytes of a password; very long passphrases are silently truncated.

Which algorithm?

OWASP's current recommendation, in order:

  1. Argon2id (winner of the Password Hashing Competition): memory-hard; use the argon2 package in Node, argon2-cffi in Python.
  2. scrypt, built into Node and Python.
  3. bcrypt, for existing systems (cost 10 or more).
  4. PBKDF2 (600,000+ iterations with SHA-256) only where FIPS compliance requires it.

Most frameworks do this for you: Django uses PBKDF2 by default and supports Argon2; Spring Security has BCryptPasswordEncoder and Argon2PasswordEncoder. Use the framework's tool rather than writing your own.

Comparing safely

Notice crypto.timingSafeEqual above. A normal === stops at the first different character, so the time a comparison takes leaks how much of a secret matched. Use constant-time comparison for hashes, tokens, API keys and signatures.

Password rules that help

Modern guidance (NIST SP 800-63B) is the opposite of the old "8 characters, one symbol, change it every 90 days":

  • Require at least 8 characters (15+ when it's the only factor), and allow at least 64; allow every character, including spaces and emoji.
  • Check new passwords against lists of breached passwords (Have I Been Pwned's API checks a password without sending it, via k-anonymity).
  • No forced composition rules and no periodic expiry; change passwords only after a suspected compromise.
  • Allow paste and password managers.
  • Rate-limit login attempts and add MFA (later lesson).
  • On login failure, say "Invalid email or password", not which one was wrong.
Node.js · editable

Practice

  1. Make hashPassword refuse passwords shorter than 8 characters with an error.
  2. Measure how long scrypt takes with N: 2 ** 14 and with N: 2 ** 15.
  3. Write needsRehash(stored) that returns true when the stored N is below the current setting.
✎ Check your understanding

1.Why add a unique random salt to each password hash?

2.Why is SHA-256 a poor choice for storing passwords?

3.Which is NOT current NIST advice?

← Authentication vs authorization