Hash Passwords With Scrypt
Hash passwords with scrypt, a random salt and explicit cost parameters, then verify them with a timing-safe comparison.
Code
import { randomBytes, scrypt, timingSafeEqual } from "node:crypto";
const SCRYPT_PARAMS = { N: 16384, r: 8, p: 1, maxmem: 64 * 1024 * 1024 } as const;
function deriveKey(password: string, salt: Buffer, length: number): Promise<Buffer> {
return new Promise((resolve, reject) => {
scrypt(password.normalize("NFKC"), salt, length, { ...SCRYPT_PARAMS }, (error, key) => {
if (error) reject(error);
else resolve(key);
});
});
}
export async function hashPassword(password: string): Promise<string> {
if (typeof password !== "string" || password.length < 8) {
throw new RangeError("password must be at least 8 characters");
}
const salt = randomBytes(16);
const key = await deriveKey(password, salt, 64);
return [
"scrypt",
SCRYPT_PARAMS.N,
SCRYPT_PARAMS.r,
SCRYPT_PARAMS.p,
salt.toString("base64"),
key.toString("base64"),
].join("$");
}
export async function verifyPassword(password: string, stored: string): Promise<boolean> {
if (typeof stored !== "string") return false;
const parts = stored.split("$");
if (parts.length !== 6 || parts[0] !== "scrypt") return false;
const [, rawN, rawR, rawP, saltText, keyText] = parts;
const expected = Buffer.from(keyText!, "base64");
const salt = Buffer.from(saltText!, "base64");
if (expected.length === 0 || salt.length === 0) return false;
let actual: Buffer;
try {
actual = await new Promise<Buffer>((resolve, reject) => {
scrypt(
password.normalize("NFKC"),
salt,
expected.length,
{ N: Number(rawN), r: Number(rawR), p: Number(rawP), maxmem: 64 * 1024 * 1024 },
(error, key) => error ? reject(error) : resolve(key),
);
});
} catch {
return false;
}
return actual.length === expected.length && timingSafeEqual(actual, expected);
}What the code does
Hash Passwords With Scrypt reduces to the functions deriveKey, hashPassword and verifyPassword.
Asynchronous boundaries use await, so failures surface as rejected promises at the call site instead of being swallowed. randomBytes, scrypt, timingSafeEqual come from node:crypto — Node.js core modules the runtime ships with. Invalid states are rejected up front: it throws RangeError instead of continuing with bad data.
What comes back is a new object, not a reference to anything passed in.
What to watch for
node:cryptoresolves inside Node.js without any package — it is a core module.- Uses
crypto, so it needs a browser context — it will not run as-is in Node without shims. - The guard
typeof password !== "string" || password.length < 8short-circuits the flow with an earlythrowwhen its condition holds.
Applying it
The primary signature is export async function hashPassword(password: string)
Treat hashPassword as the API: import it, and ignore the supporting code.
Statements that matter
actual = await new Promise<Buffer>((resolve, reject) => {
scrypt(
password.normalize("NFKC"),
salt,
expected.length,
{ N: Number(rawN), r: Number(rawR), p: Number(rawP), maxmem: 64 * 1024 * 1024 },
(error, key) => error ? reject(error) : resolve(key),
);
});
return new Promise((resolve, reject) => {
scrypt(password.normalize("NFKC"), salt, length, { ...SCRYPT_PARAMS }, (error, key) => {
if (error) reject(error);
else resolve(key);
});
});
const SCRYPT_PARAMS = { N: 16384, r: 8, p: 1, maxmem: 64 * 1024 * 1024 } as const;
Interactive Tools for This Topic
FAQ
- Does `hashPassword` modify its input?
- It does not. Inputs are only read; the return value is freshly built.
- Does the code validate its input?
- The code refuses to continue: `RangeError` is thrown while the state is still recoverable.
- 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.