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.
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
b8e7ae12510bdfb1812e463a7f086122cf37e4f7Laravel Tech Community
Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.