Current Location: Home> Latest Articles> Use hash_update_stream() in conjunction with zip:// or php://filter

Use hash_update_stream() in conjunction with zip:// or php://filter

M66 2025-05-27

In PHP, the hash_update_stream() function allows us to update a hash context that has been created. Unlike static data, streaming data can be very large or cannot be loaded into memory at once, so using streaming is very important. This article will introduce how to use the hash_update_stream() function in conjunction with zip:// or php://filter to process streaming data.

1. What is the hash_update_stream() function?

hash_update_stream() is a hash function in PHP that updates the hash context by processing streaming data such as file or network data streams. Its usage is as follows:

 hash_update_stream ( resource $context , resource $handle [, int $length = 1024 ] ) : bool
  • $context : This is the hash context created by hash_init() .

  • $handle : This is the data stream resource that needs to be read from the stream, such as a file handle or other stream types.

  • $length : This is the number of bytes read per time, defaults to 1024 bytes.

2. Use hash_update_stream() to handle zip:// stream

In PHP, zip:// is a special protocol for directly reading data streams from ZIP files. When you need to hash a compressed file, you can access the file content through the zip:// protocol and use hash_update_stream() to process the streaming data.

Suppose we have a ZIP file example.zip and it contains a text file called file.txt , we can use the following code:

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

// use zip:// Protocol read ZIP Data flow in a file
$zipStream = fopen('zip://example.zip#file.txt', 'r');

if ($zipStream) {
    // Update hash value
    while (!feof($zipStream)) {
        hash_update_stream($hashContext, $zipStream);
    }
    fclose($zipStream);

    // Get the final hash value
    $hashValue = hash_final($hashContext);
    echo "SHA-256 Hash of the file: " . $hashValue;
} else {
    echo "Unable to open the file!";
}
?>

3. Use php://filter to process streaming data

php://filter is a very powerful stream encapsulation that allows you to filter input streams. For example, you can compress, decompress or convert the data through php://filter . Combined with hash_update_stream() , you can hash the streaming data processed by some kind of filter.

Suppose we want to read and calculate the hash value of a text file through php://filter , but at the same time perform character conversion on the data. Here is an example that demonstrates how to use php://filter to read a stream converted through convert.iconv.UTF-8/ISO-8859-1 and calculate its hash:

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

// use php://filter Read and convert character encoding
$filteredStream = fopen('php://filter/read=convert.iconv.UTF-8/ISO-8859-1/resource=example.txt', 'r');

if ($filteredStream) {
    // Update hash value
    while (!feof($filteredStream)) {
        hash_update_stream($hashContext, $filteredStream);
    }
    fclose($filteredStream);

    // Get the final hash value
    $hashValue = hash_final($hashContext);
    echo "MD5 Hash of the file after conversion: " . $hashValue;
} else {
    echo "Unable to open the file!";
}
?>

4. Summary

By combining the hash_update_stream() function with the zip:// or php://filter protocol, we can flexibly hash the compressed file or processed streaming data. The streaming processing capability of hash_update_stream() enables efficient calculation of hash values ​​without consuming too much memory even if the data volume is very large. Whether reading data from a ZIP file or applying character encoding conversion in a stream, hash_update_stream() provides a simple and efficient way to handle these scenarios.