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
| Name | Type | Description |
|---|---|---|
| digest | string | Hash algorithm. |
| ikm | Buffer | KeyObject | string | Input key material. |
| salt | Buffer | string | Salt. |
| info | Buffer | string | Context label. |
| keylen | number | Output byte count. |
| callback | Function | Completion 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));