hash_hmac_file() – Generating a Keyed HMAC Hash of a File’s Contents
hash_hmac_file() is a PHP function that computes a keyed HMAC hash of a file using a specified algorithm and secret key, returning either a raw binary digest or a lowercase hexadecimal string, with detailed parameter explanations and a complete usage example.
hash_hmac_file() generates a keyed HMAC hash of the contents of a given file using the specified algorithm and secret key.
Parameters
$algo – The name of the hash algorithm to use (e.g., "md5", "sha256", "haval160,4"). See hash_algos() for a list of supported algorithms.
$filename – The path to the file whose hash is to be computed; supports fopen wrappers.
$key – The secret key used for HMAC generation.
$raw_output (optional) – Set to true to return the raw binary digest; false (default) returns a lowercase hexadecimal string.
Return value
If $raw_output is true, the function returns the raw binary digest; otherwise it returns a lowercase hexadecimal string. If the specified algorithm is not supported, FALSE is returned.
Example
<?php
// Create a file to hash
file_put_contents('example.txt', 'The quick brown fox jumped over the lazy dog.');
// Compute HMAC MD5 hash with a secret key
echo hash_hmac_file('md5', 'example.txt', 'secret');
?>Output
7eb2b5c37443418fc77c136dd20e859cSigned-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Laravel 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.
