Node Crypto Scrypt Password Hashing
Hash passwords with the built-in scrypt using random per-user salts, explicit cost parameters, and timing-safe comparison on verification.
Code
import { randomBytes, scrypt, timingSafeEqual } from 'node:crypto';
import { promisify } from 'node:util';
const scryptAsync = promisify(scrypt);
const PARAMS = { N: 16384, r: 8, p: 1, keylen: 64 };
async function hashPassword(password) {
const salt = randomBytes(16);
const derived = await scryptAsync(password.normalize('NFKC'), salt, PARAMS.keylen, PARAMS);
return `scrypt:${PARAMS.N}:${PARAMS.r}:${PARAMS.p}:${salt.toString('base64')}:\
${derived.toString('base64')}`;
}
async function verifyPassword(password, stored) {
const [scheme, N, r, p, saltB64, hashB64] = stored.split(':');
if (scheme !== 'scrypt') return false;
const salt = Buffer.from(saltB64, 'base64');
const expected = Buffer.from(hashB64, 'base64');
const actual = await scryptAsync(password.normalize('NFKC'), salt, expected.length, {
N: Number(N), r: Number(r), p: Number(p),
});
return actual.length === expected.length && timingSafeEqual(actual, expected);
}
const record = await hashPassword('correct horse battery staple');
console.log(await verifyPassword('wrong password', record));
console.log(await verifyPassword('correct horse battery staple', record));Output
Captured from a sandboxed run of this snippet in the build pipeline.
false
trueHow it works
Node Crypto Scrypt Password Hashing is built around the functions hashPassword and verifyPassword.
Every asynchronous step is awaited, so rejections surface where the call happens. randomBytes, scrypt, timingSafeEqual, promisify come from node:crypto, node:util — Node.js core modules the runtime ships with.
What to watch for
node:cryptois a Node.js built-in module — it ships with the runtime, so there is nothing to install.- Uses
crypto, so it needs a browser context — it will not run as-is in Node without shims. - The guard
scheme !== 'scrypt'short-circuits the flow with an earlyreturnwhen its condition holds.
Using it
Signature: async function hashPassword(password)
Key statements
const derived = await scryptAsync(password.normalize('NFKC'), salt, PARAMS.keylen, PARAMS);
const actual = await scryptAsync(password.normalize('NFKC'), salt, expected.length, {
N: Number(N), r: Number(r), p: Number(p),
});
return `scrypt:${PARAMS.N}:${PARAMS.r}:${PARAMS.p}:${salt.toString('base64')}:\
${derived.toString('base64')}`;
Interactive Tools for This Topic
bcrypt Cost EstimatorEstimate bcrypt cost factor latency and brute-force resistance, and get a recommended cost for your target hash time and attacker throughput.Argon2id Hash InfoEstimate Argon2id password-hash latency and memory usage, and check your parameters against OWASP 2024 minimum and recommended settings.HMAC GeneratorCompute an HMAC-SHA-256, SHA-384, or SHA-512 signature for a message and key using the Web Crypto API, with hex and Base64 output.Password Entropy EstimatorEstimate a password's alphabet size and Shannon entropy in bits, then translate that into offline crack times at realistic guessing rates.
FAQ
- Are there third-party dependencies?
- No. The imports resolve to Node.js core modules, which the runtime provides.
Report a problem with this exampleOpens a pre-filled issue on GitHub.