PHP | hash_hmac() Function

PHP hash_hmac() Function


Hello folks! welcome back to a new edition of our tutorial on PHP. In this tutorial guide, we are going to be studying about the PHP hash_hmac() Function.

The PHP hash_hmac() function is used for generating keyed hash value using HMAC method.

Syntax

Following below is the syntax to use this function -

hash_hmac ( string $algo , string $data , string $key [, bool $raw_output = FALSE ] ) : 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

data

The data you want to hash.

3

key

Secret key to generate HMAC vaiant of the message digest.

4

raw_output

By default, the value is false and hence it returns lowercase hexits values. If the value is true, it will return raw binary data.


Return Value

This built-in PHP function returns a strings of calculated message digest that will be in the form of lowercase hexits if raw_output is false, otherwise it will return raw binary data.

PHP Version

This PHP function works from PHP version greater than 5.1.2.

Example1

The following example below illustrates the usage of PHP hash_hmac() function -

<?php
   echo hash_hmac('md5', 'Welcome to Webdesigntutorialz', 'any_secretkey');
?>

Output

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

82b39d2ebabc8828e56c660f6fe7c20a

Example2

Using the PHP hash_hmac() function with ripemd128 algorithm -

<?php
   echo hash_hmac('ripemd128', 'Welcome to Webdesigntutorialz', 'any_secretkey');
?>

Output

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

286baab9161f7eb45c9a4435ff7d0a35

Example3

Following example demonstrates how to generate hash_hmac with raw_output set as TRUE -

<?php
   echo hash_hmac('ripemd128', 'Welcome to Webdesigntutorialz', 'any_secretkey', true);
?>

Output

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

ɵƋr��1�RO�F���


Alright guys! This is where we are rounding up for this tutorial post. In our next tutorial, we are going to be studying about the PHP hash_hmac_algos() Function.

Do feel free to ask your questions where necessary and we 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.