PHP | hash_hmac_file() Function
January 24, 2021
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.
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.
Syntax
Following below is the syntax to use this function -
hash_hmac_file ( string $algo , string $filename , string $key [, bool $raw_output = FALSE ] ) : string
READ: PHP | Hash Functions
Parameter Details
Sr.No | Parameter & Description |
---|---|
1 | algo 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() |
2 | filename The filepath to get the file contents. |
3 | key Secret key to generate HMAC variant of the message digest. |
4 | raw_output 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.
Example1
The following below is an example which demonstrates the usage of hash_hmac_file() function -
<?php file_put_contents('file2.txt', 'Welcome to Webdesigntutorialz'); echo hash_hmac_file('md5', 'file2.txt', 'anysecretkey'); ?>
Output
When the above code is executed, it will produce the following result -
e9768f4407dcb22fb99cb6c4449f1067
Example2
The following example shows the difference in the hash_hmac_file() output when the file contents are changed -
<?php 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'); ?>
Output
When the above code is executed, it will produce the following result -
362a60a6ef4e35f9559304a6b5372b070c97ba33cb4a747503c9c58b5c85e6db2652fb7ccf4cff91df4f08add44b93b2
Example3
The following example shows the difference in PHP hash_file() and hash_hmac_file() output -
<?php 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'); ?>
Output
When the above code is executed, it will produce the following result -
b952e8666ddb57f4b80a70041a9bc151166dd11d4aaf1393c243cf720841be77<br/><br/>b6f6f03642e61cc076d4e2def5db4048c4bb2be8308a76190e346831a6301508
READ: PHP | hash() Function
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.
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.