Skip to content
Guest
Learn Node.js · Course Syllabus Show lessons
Security & Auth · 9 min read · Intermediate

Password Hashing & Bearer Auth (scrypt)

Hash passwords safely with per-user salts using Node's built-in crypto.scryptSync and compare hashes with timingSafeEqual.

What you'll learn

  • Never store plaintext passwords or unsalted SHA-256 hashes
  • Use slow, salted password hashing (scrypt / argon2 / PBKDF2)
  • Prevent timing attacks with crypto.timingSafeEqual()

Fast hashes like MD5 or plain SHA-256 can be brute-forced at billions of guesses per second on GPUs. Always hash user passwords with a random per-user salt and a memory-hard key derivation function like crypto.scryptSync (built right into Node.js!) or Argon2, and compare digests with crypto.timingSafeEqual.

Fill-in-the-Blank Exercise

Salted Password Hash

Generate a 16-byte random salt and derive a 64-byte key with scryptSync.

const salt = crypto.(16).toString("hex");
const hash = crypto.(password, salt, 64).toString("hex");

Live Code Lab (Node.js 20)

Node.jsauth_scrypt.js
Expected OutputClick “Run Live” to compile with Node.js 20
Right pw: true
Wrong pw: false

Check Your Understanding

Score: 0 / 1

1. Why should Node.js backends use scrypt or Argon2 instead of plain SHA-256 for user passwords?

Study Workspace

Finished this lesson?

Your progress is saved locally in your browser, no account needed.

Search courses, lessons, and references

Type to search across all courses, lessons, and quick references.

Press Esc to close Powered by Pagefind