(PHP 7 >= 7.1.2, PHP 8)

hash_hkdfGenerate a HKDF key derivation of a supplied key input


    string $algo,
    string $key,
    int $length = 0,
    string $info = "",
    string $salt = ""
): string

Elenco dei parametri


Name of selected hashing algorithm (i.e. "sha256", "sha512", "haval160,4", etc..) See hash_algos() for a list of supported algorithms.


Non-cryptographic hash functions are not allowed.


Input keying material (raw binary). Cannot be empty.


Desired output length in bytes. Cannot be greater than 255 times the chosen hash function size.

If length is 0, the output length will default to the chosen hash function size.


Application/context-specific info string.


Salt to use during derivation.

While optional, adding random salt significantly improves the strength of HKDF.

Valori restituiti

Returns a string containing a raw binary representation of the derived key (also known as output keying material - OKM).


Throws a ValueError exception if key is empty, algo is unknown/non-cryptographic, length is less than 0 or too large (greater than 255 times the size of the hash function).

Log delle modifiche

Versione Descrizione
8.0.0 Now throws a ValueError exception on error. Previously, false was returned and an E_WARNING message was emitted.


Example #1 hash_hkdf() example

// Generate a random key, and salt to strengthen it during derivation.
$inputKey = random_bytes(32);
$salt = random_bytes(16);

// Derive a pair of separate keys, using the same input created above.
$encryptionKey = hash_hkdf('sha256', $inputKey, 32, 'aes-256-encryption', $salt);
$authenticationKey = hash_hkdf('sha256', $inputKey, 32, 'sha-256-authentication', $salt);

var_dump($encryptionKey !== $authenticationKey); // bool(true)

The above example produces a pair of separate keys, suitable for creation of an encrypt-then-HMAC construct, using AES-256 and SHA-256 for encryption and authentication respectively.

Vedere anche:

add a note add a note

User Contributed Notes

There are no user contributed notes for this page.
To Top