Author - StudySection Post Views - 63 views

PHP – hash_hmac() Function

Definition and Usage

The hash_hmac() function generates keyed hash values using the HMAC algorithm.
HMAC is an abbreviation for keyed-hash message authentication code or hash-based message authentication code. It makes use of cryptographic hash functions like md5, sha-256, and a secret key to return the message digest hash of the given data.


hash_hmac ( string $algo , string $data , string $key [, bool $raw_output = FALSE ] ) : string


Parameter Description
algo Name of the hashing algorithm. Hash supports a wide range of algorithms, including md5, sha256, and others.
To see the complete list of available algorithms, look for hash_hmac_algos()
data The data you want to hash.
key The secret key to generate HMAC variant of the message digest.
raw_output The value is false by default, thus it returns lowercase hexits. It will return raw binary data if the value is true.

Return Values
The hash_hmac() method returns a string with a computed message digest in lowercase hexits. If raw_output is set to false, raw binary data will be returned.
PHP Version
This function will work with PHP versions greater than 5.1.2.
Example 1
Using hash_hmac() −
echo hash_hmac('md5', 'PHP - Hash hmac() Function', 'any_secretkey');


This will produce the following result −

Example 2
Using hash_hmac() with ripemd128 algorithm −
echo hash_hmac('ripemd128', 'PHP - Hash hmac() Function', 'any_secretkey');


This will produce the following result −

Example 3
To create hash_hmac with raw output set to true −
echo hash_hmac('ripemd128', 'PHP - Hash hmac() Function', 'any_secretkey', true);


This will produce the following result −
9U'R'2a' '''

People having good command over the French language can get a French certification from StudySection. StudySection offers both beginner level and expert level French Certification Exams to test the ability to communicate in the French language.

Leave a Reply

Your email address will not be published. Required fields are marked *