PHP | hash_hkdf() Function

PHP hash_hkdf() Function


Hello dear readers! welcome back to another edition of our tutorial on PHP. In this tutorial guide, we are going to be discussing about the PHP hash_hkdf() Function.

The built-in hash_hkdf() function returns the hkdf key derivation for the given input key.

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

Syntax

Following below is the syntax to use this function -

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


Parameter Details

Sr.NoParameter & 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 Value

This PHP function returns a string of raw binary data on success and false if it fails.

PHP Version

This built-in function works from PHP version greater than 7.1.2.

Example1

Following example demonstrates the usage of the PHP hash_hkdf() function -

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

Output

When the above code is executed, it will produce the following result -

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

Example2

Using the built-in PHP hash_hkdf() function with length set to 0 -

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

Output

When the above code is executed, it will produce the following result -

8�hrx����5�����


Alright guys! This is where we are rounding up for this tutorial post. In our next tutorial, we will discuss about the hash_hmac() Function in PHP.

Do feel free to ask your questions where necessary and i will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Do follow us on our various social media handles available and also subscribe to our newsletter to get our tutorial posts delivered directly to your emails.

Thanks for reading and bye for now.