Current Location: Home> Latest Articles> How to use hash_update_stream() function to verify file integrity?

How to use hash_update_stream() function to verify file integrity?

M66 2025-05-27

In PHP, the hash_update_stream() function is a very useful tool for step-by-step computing the hash value of a data stream (such as a file). This function is usually used to verify the integrity of a file and ensure that the file is not tampered with during transmission or storage. This article will introduce how to use the hash_update_stream() function to verify the integrity of a file.

1. What is the hash_update_stream() function?

hash_update_stream() is a function provided by PHP to update the value of a hash context. Its purpose is to gradually calculate the hash value of the file by processing a given data stream (usually a file).

The syntax is as follows:

 bool hash_update_stream ( resource $context , resource $file_handle [, int $length = 8192 ] )
  • $context : hash context resource, usually created by hash_init() function.

  • $file_handle : an already opened file handle (for example, a file opened via fopen() ).

  • $length : The number of bytes read each time when reading the stream, the default is 8192 bytes.

2. Why use hash_update_stream() to verify file integrity?

File integrity verification is a key way to ensure that files are not modified or corrupted during storage or transfer. In many application scenarios, ensuring file integrity is crucial to security and data consistency.

For example, you might want to verify that files downloaded from a server have been tampered with during transfer, or check that files stored locally are not corrupted. At this point, you can confirm whether the file remains the same by comparing the hash value of the file.

3. Use hash_update_stream() to verify file integrity

We can use hash_update_stream() to read the file step by step and calculate its hash value. With this approach, we can ensure that every part of the file content is processed and finally get the full hash of the file. If a file changes anytime during transfer or storage, its hash value will change, which can be used to detect the integrity of the file.

4. Sample code

Here is a simple example showing how to use the hash_update_stream() function to verify file integrity.

 <?php
// Create a hash context
$context = hash_init('sha256');

// Open the file
$file_path = 'path/to/your/file.txt';
$file = fopen($file_path, 'rb');
if (!$file) {
    die('无法Open the file');
}

// use hash_update_stream() Gradually calculate the hash value of the file
while (!feof($file)) {
    // Each read 8192 Bytes and update the hash value
    hash_update_stream($context, $file, 8192);
}

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

// The hash value of the output file
echo "The hash value of the file is: " . $hash . "\n";

// Close the file
fclose($file);

// Assume you have previously stored the correct hash of the file
$correct_hash = 'Pre-stored correct hash value';

// Verify file integrity
if ($hash === $correct_hash) {
    echo "File integrity verification passed!\n";
} else {
    echo "File integrity verification failed!\n";
}
?>

In this example, we first create a hash context using the hash_init() function. Then, we gradually calculate the hash value of the file by opening the file and using the hash_update_stream() function. After reading the file, we use hash_final() to get the final hash value of the file and compare it with the correct hash value stored previously to verify the integrity of the file.

5. Things to note

  • The hash_update_stream() function is to read files step by step for hash calculations, which is very useful for handling large files because it does not load the entire file into memory at once.

  • Make sure the file has the correct access rights when reading, otherwise it may cause the file to fail to open or read to fail.

  • hash_update_stream() is suitable for file streams, data streams and other types of streams, and can be very conveniently used for hash calculation of streaming data.

6. Related functions

  • hash_init() : Initializes a hash context and specifies a hash algorithm (for example, sha256 ).

  • hash_update() : The value used to update the hash context.

  • hash_final() : Gets the final hash value.

7. Summary

Using the hash_update_stream() function to verify file integrity is a very effective technique. By gradually reading the file and calculating the hash value, we can ensure that the file is not tampered with or corrupted during transmission and storage. In practical applications, you can apply this method to multiple scenarios such as file download, upload verification, and file integrity check.