crypto.hkdf(digest, ikm, salt, info, keylen, callback)
digest<string> 要使用的摘要算法。ikm<string> | <ArrayBuffer> | <Buffer> | <TypedArray> | <DataView> | <KeyObject> 输入密钥材料。必须提供,但可以为空。salt<string> | <ArrayBuffer> | <Buffer> | <TypedArray> | <DataView> 盐值。必须提供,但可以为空。info<string> | <ArrayBuffer> | <Buffer> | <TypedArray> | <DataView> 额外信息值。必须提供,但可以为空,且不能超过 1024 字节。keylen<number> 要生成的密钥长度。必须大于 0。最大允许值为所选摘要函数生成的字节数乘以255(例如,sha512生成 64 字节的哈希,因此 HKDF 的最大输出为 16320 字节)。callback<Function>err<Error>derivedKey<ArrayBuffer>
HKDF 是 RFC 5869 中定义的一个简单密钥派生函数。给定的 ikm、salt 和 info 与 digest 一起用于派生长度为 keylen 字节的密钥。
【HKDF is a simple key derivation function defined in RFC 5869. The given ikm,
salt and info are used with the digest to derive a key of keylen bytes.】
提供的 callback 函数会接收两个参数:err 和 derivedKey。如果在派生密钥时发生错误,err 将被设置;否则 err 为 null。成功生成的 derivedKey 将作为 <ArrayBuffer> 传递给回调。如果任何输入参数指定了无效的值或类型,将会抛出错误。
【The supplied callback function is called with two arguments: err and
derivedKey. If an errors occurs while deriving the key, err will be set;
otherwise err will be null. The successfully generated derivedKey will
be passed to the callback as an <ArrayBuffer>. An error will be thrown if any
of the input arguments specify invalid values or types.】
import { Buffer } from 'node:buffer';
const {
hkdf,
} = await import('node:crypto');
hkdf('sha512', 'key', 'salt', 'info', 64, (err, derivedKey) => {
if (err) throw err;
console.log(Buffer.from(derivedKey).toString('hex')); // '24156e2...5391653'
});const {
hkdf,
} = require('node:crypto');
const { Buffer } = require('node:buffer');
hkdf('sha512', 'key', 'salt', 'info', 64, (err, derivedKey) => {
if (err) throw err;
console.log(Buffer.from(derivedKey).toString('hex')); // '24156e2...5391653'
});