We now have a youtube channel. Subscribe!

PHP | hash_hmac_file() Function

PHP hash_hmac_file() Function

Hello folks! welcome back to a new edition of our tutorial on PHP. In this tutorial guide, we are going to be studying about the PHP hash_hmac_file() Function.

The built-in PHP hash_hmac_file() function is used in generating keyed hash value for the given file content making use of HMAC method.

HMAC is an acronym which 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 PHP function works from PHP version greater than 5.1.2.


The following example below 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 example below shows the difference in the PHP 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 example below shows the difference in 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 guide, we are going to be discussing about the PHP hash_pbkdf2() Function.

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.

Post a Comment

Hello dear readers! Please kindly try your best to make sure your comments comply with our comment policy guidelines. You can visit our comment policy page to view these guidelines which are clearly stated. Thank you.
© 2023 ‧ WebDesignTutorialz. All rights reserved. Developed by Jago Desain