Current Location: Home> Latest Articles> How to calculate the hash value of a file using the hash_final function in PHP?

How to calculate the hash value of a file using the hash_final function in PHP?

gitbox 2025-05-20

In PHP, a hashing algorithm is a technique that converts data of any length, such as file content or strings, into fixed-length strings. Common hashing algorithms include MD5, SHA-1, SHA-256, etc. When processing files, calculating the hash value of the file can be used to verify the integrity of the file and prevent the file from being tampered with.

In PHP, we can use the hash_final function to calculate the hash value of a file. Below we will introduce how to use hash_final function to calculate the hash value of a file and use specific code examples to help you understand.

What is the hash_final function?

The hash_final function is one of the functions in PHP used to complete hash calculations. It is usually used in conjunction with hash_init and hash_update . When processing large data (such as files), it allows chunked computing hash values.

Function prototype:

 string hash_final ( resource $context [, bool $raw_output = false ] )
  • $context : This is a resource initialized by the hash_init function, which is used to store the status of the current hash calculation.

  • $raw_output : If true , return a hash value in binary format; if false , return a hash value in hexadecimal format (the default value is false ).

Use the hash_final function to calculate the hash value of a file

Here is a simple example that demonstrates how to use the hash_final function to calculate the hash value of a file.

step:

  1. Use the hash_init function to initialize the hash calculation context.

  2. Use the hash_update function to read the file in chunks and update the hash context.

  3. Use the hash_final function to get the final hash value.

Sample code:

 <?php

// File path
$file = 'path/to/your/file.txt';

// Initialize hash calculation,Select a hashing algorithm(like md5, sha256 wait)
$context = hash_init('sha256');

// Open the file and read it
$handle = fopen($file, 'rb');
if ($handle) {
    // Each read 8192 byte
    while (!feof($handle)) {
        // Read file content
        $data = fread($handle, 8192);
        // Update hash value
        hash_update($context, $data);
    }
    fclose($handle);
} else {
    echo "Unable to open the file!";
    exit;
}

// Calculate the hash value of the file
$hash = hash_final($context);

// Output hash value
echo "The hash value of the file is: " . $hash;

?>

Code parsing:

  • First, call the hash_init function to initialize the hash calculation. We choose sha256 as the hashing algorithm, and we can also use other methods such as md5 , sha1 , etc.

  • Use the fopen function to open the file, fread reads the file contents at 8192 bytes per time. This way, when processing large files, you can avoid loading the entire file into memory at one time, thereby improving efficiency.

  • hash_update will add the read data to the current hash calculation.

  • Finally, call hash_final to get the final hash value.

Notes when calculating hash values ​​using hash_final

  • Performance issues : For large files, reading files block by block instead of loading all data at once can effectively reduce memory consumption, especially when dealing with large files.

  • Hash algorithm selection : Different hash algorithms (such as md5 , sha1 , sha256 ) produce different hash values. When selecting an algorithm, it can be decided based on actual needs. For example, md5 generates a 32-character long hash value, while sha256 generates a 64-character long hash value.