FhkdfSync
Bun

function

crypto.hkdfSync

function hkdfSync(
digest: string,
salt: BinaryLike,
info: BinaryLike,
keylen: number

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 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'
@param digest

The digest algorithm to use.

@param ikm

The input keying material. Must be provided but can be zero-length.

@param salt

The salt value. Must be provided but can be zero-length.

@param info

Additional info value. Must be provided but can be zero-length, and cannot be more than 1024 bytes.

@param keylen

The length of the key to generate. Must be greater than 0. The maximum allowable value is 255 times the number of bytes produced by the selected digest function (e.g. sha512 generates 64-byte hashes, making the maximum HKDF output 16320 bytes).

Referenced types

type BinaryLike = string | NodeJS.ArrayBufferView

class KeyObject

Node.js uses a KeyObject class to represent a symmetric or asymmetric key, and each kind of key exposes different functions. The createSecretKey, createPublicKey and createPrivateKey methods are used to create KeyObjectinstances. KeyObject objects are not to be created directly using the newkeyword.

Most applications should consider using the new KeyObject API instead of passing keys as strings or Buffers due to improved security features.

KeyObject instances can be passed to other threads via postMessage(). The receiver obtains a cloned KeyObject, and the KeyObject does not need to be listed in the transferList argument.

  • asymmetricKeyDetails?: AsymmetricKeyDetails

    This property exists only on asymmetric keys. Depending on the type of the key, this object contains information about the key. None of the information obtained through this property can be used to uniquely identify a key or to compromise the security of the key.

    For RSA-PSS keys, if the key material contains a RSASSA-PSS-params sequence, the hashAlgorithm, mgf1HashAlgorithm, and saltLength properties will be set.

    Other key details might be exposed via this API using additional attributes.

  • asymmetricKeyType?: AsymmetricKeyType

    For asymmetric keys, this property represents the type of the key. See the supported asymmetric key types.

    This property is undefined for unrecognized KeyObject types and symmetric keys.

  • symmetricKeySize?: number

    For secret keys, this property represents the size of the key in bytes. This property is undefined for asymmetric keys.

  • type: KeyObjectType

    Depending on the type of this KeyObject, this property is either'secret' for secret (symmetric) keys, 'public' for public (asymmetric) keys or 'private' for private (asymmetric) keys.

  • otherKeyObject: KeyObject
    ): boolean;

    Returns true or false depending on whether the keys have exactly the same type, value, and parameters. This method is not constant time.

    @param otherKeyObject

    A KeyObject with which to compare keyObject.

  • export<T extends KeyExportOptions = {}>(
    options?: T
    ): KeyExportResult<T, NonSharedBuffer>;

    For symmetric keys, the following encoding options can be used:

    For public keys, the following encoding options can be used:

    For private keys, the following encoding options can be used:

    The result type depends on the selected encoding format, when PEM the result is a string, when DER it will be a buffer containing the data encoded as DER, when JWK it will be an object.

    When JWK encoding format was selected, all other encoding options are ignored.

    PKCS#1, SEC1, and PKCS#8 type keys can be encrypted by using a combination of the cipher and format options. The PKCS#8 type can be used with anyformat to encrypt any key algorithm (RSA, EC, or DH) by specifying acipher. PKCS#1 and SEC1 can only be encrypted by specifying a cipherwhen the PEM format is used. For maximum compatibility, use PKCS#8 for encrypted private keys. Since PKCS#8 defines its own encryption mechanism, PEM-level encryption is not supported when encrypting a PKCS#8 key. See RFC 5208 for PKCS#8 encryption and RFC 1421 for PKCS#1 and SEC1 encryption.

  • extractable: boolean,
    keyUsages: readonly KeyUsage[]

    Converts a KeyObject instance to a CryptoKey.

  • static from(

    Example: Converting a CryptoKey instance to a KeyObject:

    const { KeyObject } = await import('node:crypto');
    const { subtle } = globalThis.crypto;
    
    const key = await subtle.generateKey({
      name: 'HMAC',
      hash: 'SHA-256',
      length: 256,
    }, true, ['sign', 'verify']);
    
    const keyObject = KeyObject.from(key);
    console.log(keyObject.symmetricKeySize);
    // Prints: 32 (symmetric key size in bytes)
    

class ArrayBuffer

Represents a raw buffer of binary data, which is used to store data for the different typed arrays. ArrayBuffers cannot be read from or written to directly, but can be passed to a typed array or DataView Object to interpret the raw buffer as needed.

  • readonly [Symbol.toStringTag]: string
  • readonly byteLength: number

    Read-only. The length of the ArrayBuffer (in bytes).

  • newByteLength?: number
    ): void;

    Resizes the ArrayBuffer to the specified size (in bytes).

    MDN

    byteLength: number

    Resize an ArrayBuffer in-place.

  • begin: number,
    end?: number

    Returns a section of an ArrayBuffer.

  • newByteLength?: number

    Creates a new ArrayBuffer with the same byte content as this buffer, then detaches this buffer.

    MDN

  • newByteLength?: number

    Creates a new non-resizable ArrayBuffer with the same byte content as this buffer, then detaches this buffer.

    MDN