Cryptographic primitives
Local fixture: Node.js 22.16.0, ECMAScript modules, synthetic data only. Run node main.mjs in a disposable directory. This is not a production security implementation.
Hash a message and authenticate it with HMAC
import {createHash,createHmac,randomBytes,timingSafeEqual} from 'node:crypto';const message = 'inventory:2';const digest = createHash('sha256').update(message).digest();const key = randomBytes(32); // Disposable for this run.const mac = value => createHmac('sha256',key).update(value).digest();console.log(digest.length);console.log(timingSafeEqual(mac(message),mac(message)));console.log(timingSafeEqual(mac(message),mac('inventory:3')));Expected output:
32truefalseSHA-256 yields 32 bytes; equal-length MACs match only for the same key and message, barring a negligible collision probability.
How it works
Node’s node:crypto module provides hashes, HMAC, signatures, random generation and encryption APIs; Web Crypto exposes a different asynchronous interface. A plain digest has no secret and cannot establish the sender’s identity. An HMAC is appropriate when both parties can hold a shared secret; a signature can be verified without the signing key. Generate keys with approved libraries and secure randomness, and keep key usage, rotation and storage separate from message processing. For AEAD encryption, follow the selected algorithm’s nonce and authentication-tag requirements; never reuse a nonce under a key where uniqueness is required. The examples use public synthetic data and throwaway keys, not a production protocol. Signatures verify bytes, not business permissions or trust in an arbitrary public key.
Verify a disposable Ed25519 signature
import {generateKeyPairSync,sign,verify} from 'node:crypto';const {publicKey,privateKey} = generateKeyPairSync('ed25519');const message = Buffer.from('catalog version 2');const signature = sign(null,message,privateKey);console.log(verify(null,message,publicKey,signature));console.log(verify(null,Buffer.from('catalog version 3'),publicKey,signature));Expected output:
truefalseVerification succeeds for original bytes and fails for changed bytes. The local public key is trusted only because this fixture created it.
Common mistakes
Do not use a fast digest for password storage. Do not treat Base64 as encryption. Do not construct custom authentication protocols from primitives merely because individual operations pass tests.
Practice and next steps
Change the message in the signature fixture and predict the verification result. Explain why the public key can be shared while the private signing key cannot.
Further reading: Primary documentation. Related staged reference: Node.js crypto HMAC.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Cryptographic primitives — knowledge check
Apply the security boundary and distinguish the example from a production control.
Which primitive requires a shared secret?
What does a valid digital signature establish?
Why is a fast hash unsuitable as the sole password-storage method?