PHP - Hash hmac() Function
Definition and Usage
The hash_hmac() function is used to generate keyed hash value using HMAC method.
HMAC stands for keyed-hash message authentication code or hash-based message authentication code. It makes use of cryptographic hash function like md5, sha-256 and a secret key to return the message digest hash of the given data.
Syntax
hash_hmac ( string $algo , string $data , string $key [, bool $raw_output = FALSE ] ) : 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 |
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 Values
The hash_hmac() function returns a string containing 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 function will work from PHP Version greater than 5.1.2.
Example 1
Using hash_hmac() −
<?php
echo hash_hmac('md5', 'Welcome to Tutorialspoint', 'any_secretkey');
?>
Output
This will produce the following result −
3e89ca31da24cb046c9d11706be688c1
Example 2
Using hash_hmac() with ripemd128 algorithm −
<?php
echo hash_hmac('ripemd128', 'Welcome to Tutorialspoint', 'any_secretkey');
?>
Output
This will produce the following result −
c9b5c68b72808f31b4524fbd46bf87d0
Example 3
To generate hash_hmac with raw_output as true −
<?php
echo hash_hmac('ripemd128', 'Welcome to Tutorialspoint', 'any_secretkey', true);
?>
Output
This will produce the following result −
r1ROF