PHP hash_hmac_file() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Hash

Definition

In PHP, hash_hmac_file() reads a file and returns a keyed hash (HMAC) of its contents.

Use it to prove a file came from you and was not changed, because only the key holder can make the same value.

Syntax

Call hash_hmac_file() with the values you need. Start with this line.

PHP
$mac = hash_hmac_file('sha256', $path, $secretKey);
CallWhat it does
hash_hmac_file('sha256', 'backup.zip', 'key')Returns the 64-character HMAC of the file.
hash_hmac_file('sha256', 'missing.zip', 'key')Returns false when the file cannot be read.

It is the file version of hash_hmac().

Library declarations
PHP
hash_hmac_file(string $algo, string $filename, string $key, bool $binary = false): string|false

Parameters

ParameterDescription
algoRequired. A cryptographic algorithm such as "sha256".
filenameRequired. The path to the file.
keyRequired. The secret key.
binaryOptional. false (default) returns hex. true returns raw bytes.

Return Value

Return ValueDescription
string|falseThe HMAC of the file, or false if the file cannot be opened.

PHP Examples

Example 1 — Sign a File

Write a temp file and compute its HMAC.

PHP
<?php
$file = tempnam(sys_get_temp_dir(), 'hm_');
file_put_contents($file, 'Hello, CodeToFun!');

echo hash_hmac_file('sha256', $file, 'secret-key'), PHP_EOL;

unlink($file);

How It Works

  1. Create a temp file. It holds a known text.
  2. Pass the key. It must stay secret.
  3. Get the signature. It is 64 hex characters.
  4. Delete the file. Clean up after the demo.

Example 2 — Same as hash_hmac() on the Text

The file and the text give the same signature.

PHP
<?php
$file = tempnam(sys_get_temp_dir(), 'hm_');
file_put_contents($file, 'Hello, CodeToFun!');

$fromFile = hash_hmac_file('sha256', $file, 'secret-key');
$fromText = hash_hmac('sha256', 'Hello, CodeToFun!', 'secret-key');

echo "Match: ", $fromFile === $fromText ? "yes" : "no", PHP_EOL;

unlink($file);

How It Works

  1. Hash the file. hash_hmac_file() reads it.
  2. Hash the text. hash_hmac() uses the same words.
  3. Compare both. They are equal.
  4. Prefer the file version. It saves memory on big files.

Example 3 — Check a File Is Authentic

Edit the file and the check fails.

PHP
<?php
$file = tempnam(sys_get_temp_dir(), 'hm_');
file_put_contents($file, 'report v1');

$expected = hash_hmac_file('sha256', $file, 'secret-key');

foreach (['report v1', 'report v2'] as $content) {
    file_put_contents($file, $content);
    $actual = hash_hmac_file('sha256', $file, 'secret-key');
    echo $content, ": ", hash_equals($expected, $actual) ? "authentic" : "changed", PHP_EOL;
}

unlink($file);

How It Works

  1. Save the signature of v1. This is the expected value.
  2. Write each version. The loop changes the file.
  3. Recompute and compare. hash_equals() is timing-safe.
  4. Spot the change. v2 gives a different signature.

Frequently Asked Questions

It reads a file and returns a keyed hash of its contents using the algorithm and key you pass.
hash_hmac_file(string $algo, string $filename, string $key, bool $binary = false): string|false.
hash_file() has no key, so anyone can recompute it. hash_hmac_file() needs the secret key, so it also proves who made it.
Yes. hash_hmac_file('sha256', $path, $key) equals hash_hmac('sha256', file_get_contents($path), $key) with less memory.
false, plus a warning. Check with === false.

Next: hash_init()

hash_init() starts an incremental hashing context.

hash_init() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful