PHP Function Reference

PHP hash_hmac_file() Function



The PHP hash_hmac_file() function is used to generate a keyed hash value using the HMAC method and the contents of a given file.

HMAC stands for Hash-based Message Authentication Code. It makes use of cryptographic hash function like "md5", "sha256" and a secret key to return the message digest hash of the given data.

Syntax

hash_hmac_file(algo, data, key, binary)

Parameters

algo Required. Specify the name of selected hashing algorithm (i.e. "md5", "sha256", "haval160,4", etc..). A list of supported algorithms can be found using hash_algos() function.
data Required. Specify the URL describing location of file to be hashed. It supports fopen wrappers.
key Required. Specify the shared secret key used for generating the HMAC variant of the message digest.
binary Optional. If set to true, outputs raw binary data. Default is false which outputs lowercase hexits.

Return Value

Returns a string containing the calculated message digest as lowercase hexits unless binary is set to true in which case the raw binary representation of the message digest is returned. Returns false when algo is unknown or is a non-cryptographic hash function, or if the file data cannot be read.

Example: hash_hmac_file() example

The example below shows the usage of hash_hmac_file() function.

<?php
//creating a file to calculate hash of
file_put_contents('test.txt', 'Hello World!');

//generating a keyed hash value using the HMAC method
echo hash_hmac_file('sha1', 'test.txt', 'secretKey');
?>

The output of the above code will be:

2635ebd3e76d7fb7570d450cee1a6c45cda83dc2

❮ PHP Hash Reference