← All notes

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

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

See also

Claims

Sources

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

Permanent ID: iil:note:mjhsecsb

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.},
}

Markdown

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.

All artifact files (JSON index)

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.