Current Location: Home> Latest Articles> How to use hash_final to achieve data integrity verification in PHP?

How to use hash_final to achieve data integrity verification in PHP?

gitbox 2025-05-29

In modern web development, data integrity verification is a very important task. We need to ensure that during data transmission or storage, the data is not tampered with or corrupted. PHP provides a variety of hash-related functions to help developers achieve this requirement, among which hash_final() is one of the core functions used for hash context processing. This article will analyze in detail the purpose, working principle of hash_final() , and how to use it in actual projects for data integrity verification.

1. What is hash_final?

hash_final() is a function in the hash extension provided by PHP to obtain the final hash value in an incremental hash operation. It is usually used with hash_init() and hash_update() and is suitable for handling large files or scenarios where hash values ​​need to be calculated in segments.

Basic usage:

 string hash_final ( HashContext $context [, bool $raw_output = false ] )
  • $context : hash context created by hash_init() .

  • $raw_output (optional): If true , outputs a hash value in the original binary format, otherwise outputs a hexadecimal string.

2. Why use hash_final instead of hash()?

Although the hash() function can calculate the hash value of a string or file at one time, hash_final() is more suitable in the following scenarios:

  1. Process large files or streaming data:
    Reading the entire file into memory at once may cause memory overflow, while using hash_init() + hash_update() can read the file in segments and update the hash step by step.

  2. Multi-step processing:
    If the data is received in chunks (such as network streams or fragmented data from distributed systems), the hash can be gradually updated and finally the result can be generated with hash_final() .

  3. More flexible control:
    The hashing algorithm can be selected dynamically and the processing logic can be adjusted according to business needs in the middle.

3. Practical application: Verify file integrity

Here is an example of using hash_final() to verify file integrity:

 <?php
$file = 'largefile.zip';
$expectedHash = 'e99a18c428cb38d5f260853678922e03'; // ExpectedMD5Hash value

$context = hash_init('md5');
$handle = fopen($file, 'rb');

if (!$handle) {
    die("Unable to open the file: $file");
}

while (!feof($handle)) {
    $data = fread($handle, 8192); // Each read8KB
    hash_update($context, $data);
}

fclose($handle);

$calculatedHash = hash_final($context);

if ($calculatedHash === $expectedHash) {
    echo "File integrity verification passed。";
} else {
    echo "The file has been corrupted or tampered。";
}
?>

4. Use URL data for verification

If you need to verify the integrity of the remote file, you can use fopen() to open the remote stream (make sure that allow_url_fopen is enabled):

 <?php
$url = 'https://gitbox.net/files/sample.zip';
$expectedSha256 = '9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08';

$context = hash_init('sha256');
$handle = fopen($url, 'rb');

if (!$handle) {
    die("Unable to openURL: $url");
}

while (!feof($handle)) {
    $data = fread($handle, 8192);
    hash_update($context, $data);
}

fclose($handle);

$calculatedSha256 = hash_final($context);

if ($calculatedSha256 === $expectedSha256) {
    echo "远程File integrity verification passed。";
} else {
    echo "远程The file has been corrupted or tampered。";
}
?>