• PHP Video Tutorials

PHP - Hash hkdf() Function



Definition and Usage

The hash_hkdf() function returns HKDF key derivation for the given input key.

The HKDF is a simple key that is derived using HMAC algorithm like md5, sha256, a input key and a salt key.

Syntax

hash_hkdf ( string $algo , string $ikm [, int $length = 0 [, string $info = '' [, string $salt = '' ]]] ) : string

Parameters

Sr.No Parameter & Description
1

algo

Name of the hashing algorithm. There is a big list of algorithm available with hash, some important ones are md5, sha256, etc.

To get the full list of algorithms supported check for hash_hmac_algos()

2

ikm

Input key.

3

length

The length you want in bytes. The length cannot be greater than 255 times the chosen hash function size.

If the length is 0, then the length will be as per the chosen hash function size.

4

info

Application/context-specific info string.

5

salt

The salt secret key is required for derivation. It is optional, but using salt will add strength to HDKF derivation.

Return Values

The hash_hkdf() function returns a string of raw binary data and false if it fails.

PHP Version

This function will work from PHP Version greater than 7.1.2.

Example 1

Using hash_hkdf() −

<?php
   $inputKey = random_bytes(32);
   $salt = 'testingkey';
   $HKFD_derivation = hash_hkdf('md5', $inputKey, 32, 'aes-256-encryption', $salt);
   echo $HKFD_derivation;
?>

Output

This will produce the following result −

����E���X�eBU�\"�ڨ��ՈWu��

Example 2

Using hash_hkdf() with length 0 −

<?php
   $inputKey = random_bytes(32);
   $salt = 'testingkey';
   $HKFD_derivation = hash_hkdf('md5', $inputKey, 0, 'aes-256-encryption', $salt);
   echo $HKFD_derivation;
?>

Output

This will produce the following result −

8�hrx����5�����
php_function_reference.htm
Advertisements