PHP | hash_hmac_file() Function

PHP hash_hmac_file() Function

Hello dear readers! welcome back to another edition of our tutorial on PHP. In this tutorial guide, we are going to be discussing about the PHP hash_hmac_file() Function.

The PHP hash_hmac_file() function is used for generating keyed hash value for the given file content by using HMAC method.

HMAC is an acronym which simply stands for keyed-hash message authentication code or hash-based message authentication code. It makes use of cryptographic hash function like md5, sha-256 and a secret key to hash the file contents given.


Following below is the syntax to use this function -

hash_hmac_file ( 
   string $algo , string $filename , string $key [, bool $raw_output = FALSE ] 
: string

Parameter Details

Sr.NoParameter & Description


Name of the hashing algorithm. There is a big list of algorithm available with hash, some important ones are md5, sha256, etc.

To get the full list of algorithms supported, check for hash_hmac_algos()



The filepath to get the file contents.



Secret key to generate HMAC variant of the message digest.



By default the value is false and hence it returns lowercase hexits values. If the value is true, it will return raw binary data.

Return Value

This built-in PHP function returns a strings of calculated message digest that will be in the form of lowercase hexits if raw_output is false, otherwise it will return raw binary data.

PHP Version

This built-in function works from PHP version greater than 5.1.2.


The following below is an example which demonstrates the usage of hash_hmac_file() function -

   file_put_contents('file2.txt', 'Welcome to Webdesigntutorialz');
   echo hash_hmac_file('md5', 'file2.txt', 'anysecretkey');


When the above code is executed, it will produce the following result -



The following example shows the difference in the hash_hmac_file() output when the file contents are changed -

   file_put_contents('abc.txt', 'Hello'); 
   echo hash_hmac_file('sha256', 'abc.txt', 'mysecretkey'); 
   echo "<br/><br/>";
   file_put_contents('abc.txt', 'World');
   echo hash_hmac_file('md5', 'abc.txt', 'anysecretkey'); 


When the above code is executed, it will produce the following result -



The following example shows the difference in PHP hash_file() and hash_hmac_file() output -

   file_put_contents('filetest.txt', 'Welcome to Webdesigntutorialz');
   echo hash_file('sha256', 'filetest.txt');
   echo "<br/><br/>";
   file_put_contents('abc.txt', 'Welcome to Webdesigntutorialz'); 
   echo hash_hmac_file('sha256', 'abc.txt', 'mysecretkey'); 


When the above code is executed, it will produce the following result -


Alright guys! This is where we are rounding up for this tutorial post. In our next tutorial, we will discuss about the hash_pbkdf2() Function in PHP.

Do feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Do follow us on our various social media handles available and also subscribe to our newsletter to get our tutorial posts delivered directly to your emails.

Thanks for reading and bye for now.