← all guides

Hash passwords in Node.js: SHA-256 vs scrypt, measured

Why SHA-256 is the wrong tool for passwords, what to use instead, and how long each option really takes, with a runnable Node.js benchmark.

Store passwords with a slow, salted, memory-hard function: Argon2id if you can, scrypt if you can't. Never store them as a plain SHA-256 hash. On the test machine below, SHA-256 hashed a password in about a microsecond, roughly 270,000 times faster than scrypt at the settings OWASP recommends. That speed is what an attacker with a leaked database wants.

Every number here comes from examples/hash-passwords-nodejs/bench.mjs, using only Node's built-in node:crypto. The raw output is in results.txt beside it.

What we measured

Node.js v22.23.5 on Linux x64, an Intel Xeon at 2.10 GHz with 4 vCPUs, one hash at a time on one core:

MethodTime per hashHashes per second
SHA-256, unsalted0.00126 ms791,255
PBKDF2-HMAC-SHA256, 600,000 iterations103.08 ms10
scrypt, N=2^14, r=8, p=1 (Node's default)32.13 ms31
scrypt, N=2^17, r=8, p=1344.88 ms3

These are CPU timings on one modest server core. An attacker with GPUs computes SHA-256 far faster than this; scrypt's large memory use is what stops GPUs from running thousands of guesses in parallel cheaply.

Why fast is bad here

A password hash is only attacked after a breach. The attacker has your table of hashes and guesses passwords offline, as fast as their hardware allows. Each guess costs one hash. At 791,255 SHA-256 hashes a second on a single core, a list of a million common passwords takes about a second per user. At 3 scrypt hashes a second, the same list takes days per user, and a random salt means each user must be attacked separately.

What to use

The OWASP Password Storage Cheat Sheet recommends, in order:

  1. Argon2id, with at least 19 MiB of memory, 2 iterations and 1 degree of parallelism. Node has no Argon2 built in; the argon2 npm package provides it.
  2. scrypt, with at least N=2^17, r=8, p=1 (128 MiB of memory) when Argon2id is not available. This is built into Node.
  3. bcrypt, work factor 10 or more, for legacy systems only. Most implementations ignore input past 72 bytes.
  4. PBKDF2 with 600,000 iterations of HMAC-SHA256 when you need FIPS-140 compliance.

Note that Node's own scrypt default, N=2^14, is below the OWASP minimum. Pass the parameters explicitly.

Store and verify with scrypt

The script stores the parameters and a random 16-byte salt with each hash, so you can raise the cost later without breaking old logins:

import { scryptSync, randomBytes, timingSafeEqual } from 'node:crypto'

const SCRYPT = { N: 2 ** 17, r: 8, p: 1, maxmem: 256 * 1024 * 1024 }

function hashPassword(pw) {
  const salt = randomBytes(16)
  const key = scryptSync(pw, salt, 32, SCRYPT)
  return ['scrypt', 17, 8, 1, salt.toString('base64'), key.toString('base64')].join(':')
}

function verifyPassword(pw, stored) {
  const [, logN, r, p, salt, key] = stored.split(':')
  const test = scryptSync(pw, Buffer.from(salt, 'base64'), 32,
    { N: 2 ** Number(logN), r: Number(r), p: Number(p), maxmem: SCRYPT.maxmem })
  return timingSafeEqual(test, Buffer.from(key, 'base64'))
}

maxmem must be raised because N=2^17 with r=8 needs 128 MiB, above Node's 32 MiB default; without it, scryptSync throws. timingSafeEqual compares in constant time, so response timing does not leak how many bytes matched. The run confirmed the right password verifies, a one-letter-short password fails, and hashing the same password twice gives two different strings because of the random salt.

Mind the cost on a small server

At 345 ms of CPU and 128 MiB of memory per hash, a 1 vCPU server can check about three logins a second, and ten logins at once would want over a gigabyte of RAM. In a real app:

  • Use the async scrypt (or the argon2 package) so hashing runs on libuv's thread pool instead of blocking the event loop.
  • Rate-limit login attempts per account and per IP. That protects both your CPU and your users.
  • Tune the cost to your hardware: OWASP's lists give equivalent lower-memory settings, such as scrypt N=2^16 with p=2.

Need a strong password to test with? The password generator makes one in your browser and can also show a text's SHA-256, which is fine for checksums and exactly wrong for storing passwords.