crypto.subtle, crypto.randomUUID, crypto.getRandomValues) and a Node-compatible crypto module available via import. Both are backed by BoringSSL, so you get the same underlying primitives regardless of which API you use. The Web Crypto API is the better choice for new code; the Node-compatible module is there when you need drop-in compatibility with existing npm packages.
Web Crypto API (global)
Thecrypto global is available without any imports.
Generate a UUID
randomUUID() generates a version 4 UUID using cryptographically secure random bytes.
Fill a buffer with random bytes
Fills
typedArray in-place with cryptographically random values and returns it. Accepts any typed array (Uint8Array, Int32Array, etc.).Hash data with crypto.subtle
crypto.subtle.digest computes a hash of an ArrayBuffer or typed array.
crypto.subtle methods are all async — they return promises. Supported digest algorithms include "SHA-1", "SHA-256", "SHA-384", and "SHA-512".Generate and use a symmetric key
Node-compatible crypto module
Import crypto from node:crypto or ant:crypto when you need the Node.js API style — for example when using libraries that call createHash or randomBytes directly.
Hash with createHash
.update() calls to hash data incrementally:
HMAC with createHmac
Generate random bytes
Returns a
Buffer of size cryptographically random bytes. Synchronous.Key derivation with pbkdf2Sync
Use pbkdf2Sync to derive a key from a password for storage or comparison. Do not use a raw password hash — always use a proper KDF.
Comparison table
| Task | Web Crypto API | Node-compatible module |
|---|---|---|
| Hash | crypto.subtle.digest('SHA-256', data) | crypto.createHash('sha256').update(data).digest('hex') |
| HMAC | crypto.subtle.sign('HMAC', key, data) | crypto.createHmac('sha256', key).update(data).digest('hex') |
| Random UUID | crypto.randomUUID() | crypto.randomUUID() |
| Random bytes | crypto.getRandomValues(new Uint8Array(n)) | crypto.randomBytes(n) |
| Key derivation | crypto.subtle.deriveKey(...) | crypto.pbkdf2Sync(...) |
| Symmetric encrypt | crypto.subtle.encrypt(...) | — |