The Pbkdf2 class provides a secure way to derive cryptographic keys from a password using the PBKDF2 (Password-Based Key Derivation Function 2) algorithm.

The PBKDF2 algorithm is widely used for generating keys from passwords, as it applies a pseudorandom function to the input password along with a salt value and iterates the process multiple times to increase the key's resistance to brute-force attacks.

This class offers a single static method deriveKey to perform key derivation.

Example

// Key Derivation
const derivedKey = await Pbkdf2.deriveKey({
hash: 'SHA-256', // The hash function to use ('SHA-256', 'SHA-384', 'SHA-512')
password: new TextEncoder().encode('password'), // The password as a Uint8Array
salt: new Uint8Array([...]), // The salt value
iterations: 1000, // The number of iterations
length: 256 // The length of the derived key in bits
});

Remarks

This class relies on the availability of the Web Crypto API.

Constructors

Methods

Constructors

Methods

  • Derives a cryptographic key from a password using the PBKDF2 algorithm.

    Parameters

    Returns Promise<Uint8Array>

    A Promise that resolves to the derived key as a Uint8Array.

    Remarks

    This method applies the PBKDF2 algorithm to the provided password along with a salt value and iterates the process a specified number of times. It uses a cryptographic hash function to enhance security and produce a key of the desired length. The method is capable of utilizing either the Web Crypto API or the Node.js Crypto module, depending on the environment's support.

    Example

    const derivedKey = await Pbkdf2.deriveKey({
    hash: 'SHA-256',
    password: new TextEncoder().encode('password'),
    salt: new Uint8Array([...]),
    iterations: 1000,
    length: 256
    });