PHP Function Reference

PHP hash_hmac() Function



The PHP hash_hmac() function is used to generate a keyed hash value using the HMAC method.

HMAC stands for Hash-based Message Authentication Code. It makes use of cryptographic hash function like "md5", "sha256" and a secret key to return the message digest hash of the given data.

Syntax

hash_hmac(algo, data, key, binary)

Parameters

algo Required. Specify the name of selected hashing algorithm (i.e. "md5", "sha256", "haval160,4", etc..). A list of supported algorithms can be found using hash_algos() function.
data Required. Specify the message to be hashed.
key Required. Specify the shared secret key used for generating the HMAC variant of the message digest.
binary Optional. If set to true, outputs raw binary data. Default is false which outputs lowercase hexits.

Return Value

Returns a string containing the calculated message digest as lowercase hexits unless binary is set to true in which case the raw binary representation of the message digest is returned. Returns false when algo is unknown or is a non-cryptographic hash function.

Example: hash_hmac() example

The example below shows the usage of hash_hmac() function.

<?php
//generating a keyed hash value using the HMAC method
echo hash_hmac('sha1', 'Hello World!', 'secretKey');
?>

The output of the above code will be:

2635ebd3e76d7fb7570d450cee1a6c45cda83dc2

❮ PHP Hash Reference