Current Location: Home> Latest Articles> Use php://stdin and hash_update_stream() command line processing

Use php://stdin and hash_update_stream() command line processing

M66 2025-05-27

In PHP, when processing large files, directly loading the entire file into memory for hash calculations may take up a lot of memory resources, especially when the files are very large. To solve this problem, PHP provides the php://stdin stream and hash_update_stream() function. The combination of the two can implement streaming hash processing, that is, hash calculation while reading the file content to avoid memory overflow.

This article will demonstrate how to use these two tools for streaming hash calculations on the command line, especially when working with large files, how to efficiently calculate hash values.

1. Preparation

First, make sure you have PHP installed and you are able to run PHP scripts from the command line. You can confirm whether PHP is installed correctly by following the following command:

 php -v

If you have not installed PHP, you can download and install PHP at http://m66.net/download/php .

Next, we will use PHP's built-in php://stdin stream to read standard input and combine the hash_update_stream() function for hash calculation.

2. Use php://stdin and hash_update_stream() for streaming hashing

Introduction to php://stdin

php://stdin is a special stream that allows you to read data from standard input (usually the input stream from the command line). You can use it to handle large files or live input streams without loading the entire data into memory.

hash_update_stream() function

The hash_update_stream() function is one of the functions used by PHP to calculate hash values. Unlike the regular hash_update() function, it allows you to update the hash value directly through the stream, which makes it particularly useful when dealing with big data.

Function prototype:

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

  • $stream : The stream used to calculate hash (in our case, php://stdin ).

  • $length : The maximum number of bytes read at a time (default is 8192 bytes).

3. Sample code

Here is a simple example showing how to use php://stdin and hash_update_stream() for streaming hash calculations.

Example: Read and calculate hash from standard input

 <?php

// Create a SHA256 hash context
$context = hash_init('sha256');

// Open php://stdin flow
$stream = fopen('php://stdin', 'r');

// Each read 8192 Bytes of data and update hash value
while (!feof($stream)) {
    $data = fread($stream, 8192);
    hash_update_stream($context, $data);
}

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

// Output the calculated hash value
echo "The calculated hash value is:$hash\n";

// 关闭flow
fclose($stream);

?>

How to run

  1. Save the above code as hash_stdin.php .

  2. Pipe data to the script for hash calculations on the command line. For example, read a file and calculate its SHA256 hash:

 cat largefile.txt | php hash_stdin.php

Alternatively, you can directly have hash calculations by entering:

 echo "Hello, World!" | php hash_stdin.php

4. Code explanation

  1. Create a hash context:
    Create a SHA256 hash context using hash_init('sha256') . You can replace 'sha256' with other supported hashing algorithms such as md5 , sha1 , etc.

  2. Open the standard input stream:
    fopen('php://stdin', 'r') opens a stream pointing to standard input. This way, we can read the input data from the command line.

  3. Read and update the hash:
    Use fread() to read the data block by block (8192 bytes are read each time), and then use hash_update_stream() to update the hash calculation.

  4. Calculate the final hash value:
    Once the file is read, call hash_final() to calculate and get the final hash value.

  5. Output hash value:
    Use echo to output the calculated hash value to the command line.

5. Applicable scenarios

  • Handling large files: Using php://stdin and hash_update_stream() is an ideal solution when you need to process large files without wanting to load them completely into memory. It allows you to read data step by step and perform hash calculations.

  • Real-time data stream: This is also possible if you need to hash the real-time data stream, such as data streams obtained from a network connection or sensor device.

6. Things to note

  • When dealing with very large files, make sure that the number of bytes read ( length ) is set reasonably. By default, fread() reads up to 8192 bytes of data at a time, and can be adjusted to a larger value to improve performance if needed.

  • When using streaming, always make sure to close the streaming resources after the operation is completed to avoid resource leakage.

7. Conclusion

By combining php://stdin and hash_update_stream() , you can handle hash calculations of large files or real-time data streams very efficiently. This method not only saves memory, but also allows for simple and fast hash calculations on the command line. If you are working on a huge file or streaming data, this streaming method is highly recommended.

Hope this article helps you understand how to use PHP for streaming hashing on the command line. If you have any questions or further questions, feel free to ask me!