Crypto

crypto.hkdf()

Derives purpose-separated key material from input keying material. HKDF is not a password hashing function. Use distinct context labels for distinct purposes. Tested on Node.js 22.16.0. Example output (one line per entry): ["32 false"].

Formal Syntax & Structure

nodejs syntaxnodejs
crypto.hkdf(digest, ikm, salt, info, keylen, callback)

Parameters & Attributes

NameTypeDescription
digeststringHash algorithm.
ikmBuffer | KeyObject | stringInput key material.
saltBuffer | stringSalt.
infoBuffer | stringContext label.
keylennumberOutput byte count.
callbackFunctionCompletion callback.

Return Value

undefined

Derived ArrayBuffer delivered to callback.

Working Example

Example Snippetnodejs
import { hkdf, randomBytes } from 'node:crypto';import { promisify } from 'node:util';const derive = promisify(hkdf), ikm = randomBytes(32), salt = randomBytes(32);const a = Buffer.from(await derive('sha256', ikm, salt, 'book:read', 32)), b = Buffer.from(await derive('sha256', ikm, salt, 'book:write', 32));console.log(a.length, a.equals(b));
Node.js Reference