Password verification and secrets
Local fixture: Node.js 22.16.0, ECMAScript modules, synthetic data only. Run node main.mjs in a disposable directory. This is not a production security implementation.
Derive and compare synthetic password material
import {scrypt,randomBytes,timingSafeEqual} from 'node:crypto';import {promisify} from 'node:util';const derive = promisify(scrypt);const salt = randomBytes(16);// Lab-only work factor; not a production recommendation.const options = {N:16384,r:8,p:1,maxmem:32*1024*1024};const expected = await derive('fixture-password',salt,32,options);for (const sample of ['fixture-password','different-fixture']) { const actual = await derive(sample,salt,32,options); console.log(timingSafeEqual(expected,actual));}Expected output:
truefalseThe same password and parameters produce matching bytes; a different synthetic input does not. No password material or derived bytes are printed.
How it works
Prefer a reviewed Argon2id password-hashing library where supported; scrypt is a memory-hard alternative when Argon2id is unavailable. Select parameters against current guidance and measured service capacity, use unique random salts, record algorithm and version, and rehash when policy changes. Node 22.16.0 exposes asynchronous scrypt(password, salt, keylen, options, callback). The example uses explicit small lab parameters only to demonstrate the API, not a production password policy. Enforce bounded input and concurrency before invoking a costly KDF. Compare equal-length derived byte sequences with timingSafeEqual; this alone does not make an entire login flow timing-safe. Prefer a maintained verifier and generic login failure responses. For operational secrets use a trusted provider, restrict access and avoid dumping environment values or writing secrets to logs.
Project safe diagnostic metadata
import assert from 'node:assert/strict';const config = {provider:'fixture-vault', secret:'synthetic-value', keyVersion:2};function diagnostic(c) { return {provider:c.provider,keyVersion:c.keyVersion}; }assert.deepEqual(diagnostic(config),{provider:'fixture-vault',keyVersion:2});console.log(JSON.stringify(diagnostic(config)));Expected output:
{"provider":"fixture-vault","keyVersion":2}An allowlisted diagnostic projection avoids logging the secret field; a real logger must enforce redaction across all paths.
Common mistakes
Never store plaintext or use unsalted SHA-256 for passwords. Do not use the demonstration work factor for production. A salt does not need to be hidden; do not confuse it with an optional separately protected pepper. Do not print real passwords or hashes.
Practice and next steps
Explain which stored fields are needed to verify a password after an algorithm parameter change. Design a rehash-on-success policy without attempting real logins.
Further reading: Primary documentation. Related staged reference: Password derivation and verification.
API details: Node.js crypto documentation.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Password verification and secrets — knowledge check
Apply the security boundary and distinguish the example from a production control.
What is the role of a per-password salt?
Why bound concurrent password verification?
Where should real application secrets appear?