---
source: https://iinventedlight.com/library/notes/node-crypto-hash-random.md
title: "Node.js crypto basics - hashes, HMAC, random UUIDs and constant-time comparison"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "MIT (Node.js API docs); MIT (nodejs/node (GitHub)); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "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."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-18
status: published
approved_by: "Cole Withington, 2026-11-20 13:14 PT: \"Publish the full batch and rewrite the 6 live notes\""
checked_on: 2026-11-18
as_of: 2026-11-18
confidence: medium
untested: "Not reproduced (2026-11-18). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-web: node crypto"
tags: [node, crypto, hashing, hmac, random, uuid]
client: Librarian
sources:
  - id: S1
    title: "Crypto"
    publisher: Node.js API docs
    url: https://nodejs.org/api/crypto.html
    accessed: 2026-11-18
    license: "MIT"
  - id: S2
    title: "doc/api/crypto.md"
    publisher: nodejs/node (GitHub)
    url: https://github.com/nodejs/node/blob/main/doc/api/crypto.md
    accessed: 2026-11-18
    license: "MIT"
---
# 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

```js
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; set `disableEntropyCache: true` to skip the cache.
- `randomBytes(size)` returns a `Buffer` synchronously, or calls `callback(err, buf)` asynchronously if you pass one.
- `randomInt(min, max)` returns `n` with `min <= n < max` and 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

- `Hmac` objects are created with `createHmac()`, not with the `new` keyword.
- Node.js can be built without the `node:crypto` module; then `require('node:crypto')` throws, which CommonJS code can catch with try/catch.

## See also

- [node-process-and-console](https://iinventedlight.com/library/notes/node-process-and-console/)
- [node-esm](https://iinventedlight.com/library/notes/node-esm/)
- [node-http-and-url](https://iinventedlight.com/library/notes/node-http-and-url/)

## 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.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
