crypto.hkdfSync(digest, ikm, salt, info, keylen)


提供了一个同步的 HKDF 密钥派生函数,如 RFC 5869 中定义的。给定的 ikm、salt 和 info 会与 digest 一起用来派生一个 keylen 字节的密钥。可用的摘要算法取决于 OpenSSL 的版本和配置。HKDF 内部使用 HMAC。crypto.getHashes() 列出了哈希 API 可用的算法,但并不是每个列出的算法都适合用于 HMAC 或 HKDF。

🌐 Provides a synchronous HKDF key derivation function as defined in RFC 5869. The given ikm, salt and info are used with the digest to derive a key of keylen bytes. The available digest algorithms depend on the version and configuration of OpenSSL. HKDF uses HMAC internally. crypto.getHashes() lists algorithms available to the hashing APIs, but not every listed algorithm is necessarily suitable for HMAC or HKDF.

成功生成的 derivedKey 将作为 <ArrayBuffer> 返回。

🌐 The successfully generated derivedKey will be returned as an <ArrayBuffer>.

如果任何输入参数指定了无效的值或类型,或者无法生成派生密钥,将会抛出错误。

🌐 An error will be thrown if any of the input arguments specify invalid values or types, or if the derived key cannot be generated.

import { Buffer } from 'node:buffer';
const {
  hkdfSync,
} = await import('node:crypto');

const derivedKey = hkdfSync('sha512', 'key', 'salt', 'info', 64);
console.log(Buffer.from(derivedKey).toString('hex'));  // '24156e2...5391653'const {
  hkdfSync,
} = require('node:crypto');
const { Buffer } = require('node:buffer');

const derivedKey = hkdfSync('sha512', 'key', 'salt', 'info', 64);
console.log(Buffer.from(derivedKey).toString('hex'));  // '24156e2...5391653'