hash_hkdf()
hash_hkdf() — Generate a HKDF key derivation of a supplied key input.
Built-in function of PHP 8.5. The manual lists it as available in PHP 7 >= 7.1.2, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.
Signature
hash_hkdf(string $algo, string $key, int $length = 0, string $info = "", string $salt = ""): string
Parameters
| Parameter | Type | What it takes |
|---|---|---|
| $algo | string | Name of selected hashing algorithm (e.g. "sha256"). For a list of supported algorithms see hash_hmac_algos(). Note: Non-cryptographic hash functions are not allowed. |
| $key | string | Input keying material (raw binary). Cannot be empty. |
| $length | int | Desired output length in bytes. Cannot be greater than 255 times the chosen hash function size. |
| $info | string | Application/context-specific info string. |
| $salt | string | Salt to use during derivation. |
Return value
Returns a string containing a raw binary representation of the derived key (also known as output keying material - OKM).
Example
A runnable snippet:
// 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)
Changelog
| Version | Change |
|---|---|
| 8.0.0 | Now throws a ValueError exception on error. Previously, false was returned and an E_WARNING message was emitted. |
See also
All PHP 8.5 built-in functions
Reference card built from the PHP manual page hash_hkdf() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.