PHP hash_hkdf() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Hash

Definition

In PHP, hash_hkdf() derives a new key from a strong secret using HKDF (HMAC-based key derivation).

Use it to split one master key into separate keys, such as one for encryption and one for signing.

Syntax

Call hash_hkdf() with the values you need. Start with this line.

PHP
$key = hash_hkdf('sha256', $masterKey, 32, 'encryption', $salt);
CallWhat it does
hash_hkdf('sha256', $key, 32, 'enc')Returns 32 raw bytes for encryption.
hash_hkdf('sha256', $key, 32, 'auth')Returns a different 32-byte key for signing.

The result is raw binary. Use bin2hex() to print it.

Library declarations
PHP
hash_hkdf(string $algo, string $key, int $length = 0, string $info = "", string $salt = ""): string

Parameters

ParameterDescription
algoRequired. A cryptographic algorithm such as "sha256". Checksums like crc32 are not allowed.
keyRequired. The input key. It must not be empty.
lengthOptional. Bytes to return. 0 (default) gives the hash size. The maximum is 255 times the hash size.
infoOptional. A label for the key's purpose, such as "encryption".
saltOptional. A salt. If empty, a string of zero bytes is used.

Return Value

Return ValueDescription
stringThe derived key as raw bytes. A bad algorithm, empty key or bad length throws ValueError.

PHP Examples

Example 1 — Derive a 32-Byte Key

Turn a master key into a new key, then print it as hex.

PHP
<?php
$key = hash_hkdf('sha256', 'master-secret', 32, 'encryption', 'salt-123');

echo bin2hex($key), PHP_EOL;

How It Works

  1. Pass the master key. It should be strong and random.
  2. Ask for 32 bytes. That suits a 256-bit key.
  3. Add info and salt. They shape the result.
  4. Convert with `bin2hex()`. The raw bytes become 64 hex digits.

Example 2 — Different Info Gives Different Keys

One master key, two purposes.

PHP
<?php
$enc  = hash_hkdf('sha256', 'master-secret', 32, 'enc');
$auth = hash_hkdf('sha256', 'master-secret', 32, 'auth');

echo "Key length: ", strlen($enc), " bytes", PHP_EOL;
echo "Keys differ: ", $enc !== $auth ? "yes" : "no", PHP_EOL;

How It Works

  1. Use one master key. Both calls share it.
  2. Change only the info label. enc and auth.
  3. Get two keys. Each is 32 bytes.
  4. Use each key for one job. Never reuse a key for two purposes.

Example 3 — Default Length

Length 0 returns one hash-size block.

PHP
<?php
foreach (['sha256', 'sha512'] as $algo) {
    $key = hash_hkdf($algo, 'master-secret');
    echo $algo, ": ", strlen($key), " bytes", PHP_EOL;
}

How It Works

  1. Skip the length. It defaults to 0.
  2. PHP uses the hash size. sha256 is 32 bytes.
  3. A bigger hash gives a bigger key. sha512 is 64 bytes.
  4. Set the length yourself when you need a specific size.

Frequently Asked Questions

It derives a new key from a strong input key using the HKDF method. Different info labels give different, unrelated keys.
hash_hkdf(string $algo, string $key, int $length = 0, string $info = "", string $salt = ""): string.
No. HKDF is fast and expects a strong random key. Use password_hash() or hash_pbkdf2() for passwords.
It is raw binary bytes. Use bin2hex() to see it as hex or base64_encode() to store it as text.
It labels the purpose of the key. Using "enc" and "auth" from one master key gives two separate keys.

Next: hash_hmac()

hash_hmac() creates a keyed hash to sign and check data.

hash_hmac() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful