Using hash_hmac() to Generate HMAC Hashes with a Key in PHP

This article explains PHP’s hash_hmac() function, detailing its signature, the required algorithm, data, key, and raw_output parameters, describing the returned hash format, and providing a complete example with code and the resulting hash output.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
Using hash_hmac() to Generate HMAC Hashes with a Key in PHP

The hash_hmac() function in PHP generates a keyed hash value using the HMAC method. Its signature is

string hash_hmac(string $algo, string $data, string $key, bool $raw_output = false)

.

Parameters

algo : The name of the hash algorithm to use (e.g., "md5", "sha256", "haval160,4"). Use hash_algos() to retrieve the list of supported algorithms.

data : The message to be hashed.

key : The secret key used for HMAC generation.

raw_output : Set to true to return raw binary data; set to false (default) to return a lowercase hexadecimal string.

Return value

If $raw_output is true, the function returns the raw binary hash; otherwise it returns a lowercase hexadecimal string. If the specified algorithm is not supported, hash_hmac() returns false.

Example

<?php
echo hash_hmac('ripemd160', 'The quick brown fox jumped over the lazy dog.', 'secret');
?>

Output

b8e7ae12510bdfb1812e463a7f086122cf37e4f7
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PHPHMAChash_hmac
Laravel Tech Community
Written by

Laravel Tech Community

Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.