Node.js crypto basics - hashes, HMAC, random UUIDs and constant-time comparison
Answer: Import createHash from node:crypto and call .update(data).digest('hex') on it. The node:crypto module provides cryptographic functionality that includes wrappers for OpenSSL's hash, MAC, cipher, sign and verify functions.
Examples
import {
createHash, createHmac, hash, randomUUID, randomBytes, randomInt, timingSafeEqual,
} from 'node:crypto';
const digest = createHash('sha256').update('some data').digest('hex');
const quick = hash('sha256', 'some data'); // one-shot, v21.7.0+
const mac = createHmac('sha256', 'abcdefg').update('I love cupcakes').digest('hex');
const id = randomUUID(); // RFC 4122 version 4
const token = randomBytes(32).toString('hex');
const die = randomInt(1, 7); // 1 <= n < 7
const a = Buffer.from(mac, 'hex');
const b = Buffer.from(receivedMac, 'hex');
const ok = a.length === b.length && timingSafeEqual(a, b);
Randomness
randomUUID()generates a random RFC 4122 version 4 UUID using a cryptographic pseudorandom number generator. Node.js caches enough random data for up to 128 UUIDs; setdisableEntropyCache: trueto skip the cache.randomBytes(size)returns aBuffersynchronously, or callscallback(err, buf)asynchronously if you pass one.randomInt(min, max)returnsnwithmin <= n < maxand avoids modulo bias; the range must be less than 2^48.
Comparing secrets
timingSafeEqual(a, b) compares bytes with a constant-time algorithm, so an attacker cannot use timing to guess either value. Use it for HMAC digests or authentication cookies. Both inputs must have the same byte length, or an error is thrown, so check lengths first.
Rules
Hmacobjects are created withcreateHmac(), not with thenewkeyword.- Node.js can be built without the
node:cryptomodule; thenrequire('node:crypto')throws, which CommonJS code can catch with try/catch.
See also
Claims
- id: C1 claim: timingSafeEqual uses a constant-time algorithm. source: S2 evidence: "using a constant-time algorithm."
- id: C2 claim: randomInt avoids modulo bias. source: S2 evidence: "This implementation avoids [modulo bias][]."
- id: C3 claim: Inputs to timingSafeEqual must have equal byte length. source: S1 evidence: "An error is thrown if a and b have different byte lengths."
Sources
- [S1] Crypto. Node.js API docs. https://nodejs.org/api/crypto.html . License: MIT. Accessed 2026-11-18.
- [S2] doc/api/crypto.md. nodejs/node (GitHub). https://github.com/nodejs/node/blob/main/doc/api/crypto.md . License: MIT. Accessed 2026-11-18.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Node.js API docs, Crypto; nodejs/node (GitHub), doc/api/crypto.md. MIT (https://raw.githubusercontent.com/nodejs/node/main/LICENSE). Copyright Node.js contributors. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Node.js API docs, Crypto; nodejs/node (GitHub), doc/api/crypto.md. MIT (https://raw.githubusercontent.com/nodejs/node/main/LICENSE). Copyright Node.js contributors.
Cite this
iinventedlight. 2026. "Node.js crypto basics - hashes, HMAC, random UUIDs and constant-time comparison." iinventedlight Library, iil:note:mjhsecsb, version of 2026-11-18. https://iinventedlight.com/library/notes/node-crypto-hash-random/. License: CC-BY-4.0.
@misc{iil_note_mjhsecsb,
author = {{iinventedlight}},
title = {{Node.js crypto basics - hashes, HMAC, random UUIDs and constant-time comparison}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-18},
url = {https://iinventedlight.com/library/notes/node-crypto-hash-random/},
note = {Permanent ID iil:note:mjhsecsb. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
Extra files beside this note
These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.
- node-crypto-hash-random.table.json (table; CC-BY-4.0)
- node-crypto-hash-random.schema.json (schema; CC-BY-4.0)
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/node-crypto-hash-random.md.