Tuesday, October 7, 2025
HomeLanguagesPHP | hash_hmac_file() Function

PHP | hash_hmac_file() Function

The hash_hmac_file() function is an inbuilt function in PHP that is used to generate a keyed hash value using the contents of a given file. 

Syntax:

string hash_hmac_file( $algo, $file, $key, $raw_opt )

Parameters: This function accepts four parameters as mentioned above and described below.

  • $algo: It is the required parameter that specifies the selected hashing algorithm.
  • $file: This parameter is used to specify the file URL to be hashed.
  • $key: This parameter is used to hold the shared secret key used for generating the HMAC.
  • $raw_opt: If the parameter is set to true then output will be raw binary data and if the parameter is set to False then output will be lowercase exits.

Return Value: This function returns a string containing the calculated message digest as lowercase exits. The below programs use the file gfg.txt and the contents of the file are:

neveropen A Computer Science Portal for Geeks

The below programs illustrate the hash_hmac_file() function in PHP.

Program 1: 

php




<?php
 
// PHP program to illustrate
//  hash_hmac_file function
 
 
// Create a file to calculate hash of
file_put_contents('gfg.txt', 'Geeks');
 
// Display result
echo hash_hmac_file('sha1', 'gfg.txt',
            'password', false);
?>


Output:

a5365a345a41ac0780bf63e4d33576560b86163c

Program 2: 

php




<?php
 
// PHP program to illustrate
//  hash_hmac_file function
 
// Create a file to calculate hash of
file_put_contents('gfg.txt', 'Geeks');
 
// Display result
echo hash_hmac_file('sha256', 'gfg.txt', 'password') . "</br>";
 
 
// Create a file to calculate hash of
file_put_contents('gfg.txt', 'Content');
 
// Display result
echo hash_hmac_file('md5', 'gfg.txt', 'password', false);
?>


Output:

a73af6923445a30fbacd646622b254069f90c2502e63b1025918aa93f2ddca9d
a7b2b24ac2334070c42a852fb5ef0c92 

Reference: http://php.net/manual/en/function.hash-file.php

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32340 POSTS0 COMMENTS
Milvus
86 POSTS0 COMMENTS
Nango Kala
6708 POSTS0 COMMENTS
Nicole Veronica
11872 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11936 POSTS0 COMMENTS
Shaida Kate Naidoo
6829 POSTS0 COMMENTS
Ted Musemwa
7090 POSTS0 COMMENTS
Thapelo Manthata
6780 POSTS0 COMMENTS
Umr Jansen
6784 POSTS0 COMMENTS