Skip to main content
Random values, digests, HMAC signing/verification, native off-loop bcrypt, and base64. Everything is synchronous except crypto.subtle methods (which resolve on the microtask queue) and crypto.bcrypt (which runs off the event loop on a dedicated thread pool).

crypto.randomUUID()

Use it for request ids, session ids, and primary keys:

crypto.getRandomValues(typedArray)

Non-typed arguments throw:

crypto.subtle.digest(algorithm, data)

Errors are TypeErrors thrown synchronously (before the Promise):

Hashing a stream of chunks

crypto.subtle HMAC (importKey, sign, verify, exportKey)

crypto.subtle supports HMAC keys and constant-time verification across SHA-1, SHA-256, SHA-384, and SHA-512:
Asymmetric algorithms (RSA, ECDSA, Ed25519), symmetric encryption (encrypt / decrypt), and key generation (generateKey) are not built in. Use ffi with a native library when you need those primitives.

crypto.bcrypt (hash, verify)

Native promise-based bcrypt password hashing running on a dedicated background worker pool off the event loop. Hashing or verifying passwords will never starve or stutter HTTP traffic, WebSocket connections, or timer callbacks.

Safety & Constraints

  • Passwords ≤ 72 bytes: Per bcrypt design, passwords exceeding 72 bytes are rejected (Error: bcrypt: password exceeds 72 bytes).
  • Bounded worker queue: Backed by a dedicated thread pool (2–4 threads with 256 KiB stacks) and a fixed 16-slot ring queue. If all 16 slots are busy, calls reject immediately with Error: crypto.bcrypt: queue full rather than growing unbounded memory.
  • Off-event-loop execution: Key derivation and crypt rounds execute strictly on worker threads. CSPRNG salt generation occurs synchronously on dispatch; no allocations occur on the hot path after initialization.
  • Main-thread only: Calling crypto.bcrypt inside a Worker thread throws TypeError: crypto.bcrypt is not available in workers.
  • No sync methods: hashSync and verifySync are intentionally not provided to prevent developers from blocking the event loop.

btoa / atob (globals)

btoa deviates from the spec in your favour: non-Latin-1 input does not throw — it encodes the UTF-8 bytes, and atob decodes them straight back, so Unicode round-trips with no workaround:
atob does throw on malformed input:

Not provided

Gotchas

  • crypto.subtle methods compute synchronously and resolve on the microtask queue — awaiting them does not free the loop for I/O.
  • getRandomValues is Uint8Array-only-ish: any TypedArray works, plain arrays/strings throw.
  • btoa / atob don’t throw on non-Latin-1 — both sides go through UTF-8, so Unicode round-trips (unlike browsers).
  • crypto.subtle keys only support "raw" HMAC — for TLS server certificates, see TLS.

See also