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.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
hash_hmac_file() – Generating a Keyed HMAC Hash of a File’s Contents

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

7eb2b5c37443418fc77c136dd20e859c
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.

PHPHMACFile Hashinghash_hmac_file
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.