PHP hash_update_stream() Function

Definition
In PHP, hash_update_stream() reads data from an open stream, such as a file handle, and adds it to a hashing context.
It returns how many bytes it added, and you can limit how many to read.
Syntax
Call hash_update_stream() with the values you need. Start with this line.
$bytes = hash_update_stream($context, $stream);
| Call | What it does |
|---|
hash_update_stream($ctx, $fp) | Reads to the end of the stream. |
hash_update_stream($ctx, $fp, 5) | Reads at most 5 bytes. |
The stream moves forward as it is read. Call rewind() first if it was written to.
Library declarations
hash_update_stream(HashContext $context, resource $stream, int $length = -1): int
Parameters
| Parameter | Description |
|---|
| context | Required. A HashContext from hash_init(). |
| stream | Required. An open stream resource, such as from fopen(). |
| length | Optional. Maximum bytes to read. -1 (default) reads to the end. |
Return Value
| Return Value | Description |
|---|
| int | The number of bytes added to the hash. |
PHP Examples
Example 1 — Hash a Stream
Write to memory, rewind, and hash it.
<?php
$fp = fopen('php://memory', 'r+');
fwrite($fp, 'Hello, CodeToFun!');
rewind($fp);
$ctx = hash_init('sha256');
$bytes = hash_update_stream($ctx, $fp);
echo "Bytes: ", $bytes, PHP_EOL;
echo "Same as hash(): ", hash_final($ctx) === hash('sha256', 'Hello, CodeToFun!') ? "yes" : "no", PHP_EOL;
fclose($fp);
Bytes: 17
Same as hash(): yes
How It Works
- Open a memory stream. Write the text to it.
- Rewind to the start. Reading begins there.
- Call `hash_update_stream()`. It returns the byte count.
- Finish and compare. The hash equals
hash() of the text.
Example 2 — Limit the Bytes
Read only the first 5 bytes.
<?php
$fp = fopen('php://memory', 'r+');
fwrite($fp, 'Hello, CodeToFun!');
rewind($fp);
$ctx = hash_init('sha256');
$bytes = hash_update_stream($ctx, $fp, 5);
echo "Bytes: ", $bytes, PHP_EOL;
echo "Matches 'Hello': ", hash_final($ctx) === hash('sha256', 'Hello') ? "yes" : "no", PHP_EOL;
fclose($fp);
Bytes: 5
Matches 'Hello': yes
How It Works
- Pass 5 as the length. Only 5 bytes are read.
- Check the count. It returns 5.
- Finish. The context holds
Hello. - Compare. It equals
hash() of Hello.
Frequently Asked Questions
It reads data from an open stream and adds it to a hashing context, then returns the number of bytes it added.
hash_update_stream(HashContext $context, resource $stream, int $length = -1): int.
Pass a length. For example 5 reads at most 5 bytes. Call it again to continue from where it stopped.
The stream was already at its end. Use rewind() to go back to the start before you call it.
Use it for streams that are not plain files, such as php://memory, sockets or uploads you already opened.
CODETOFUN NEWS
Stay Ahead in Tech
Get the latest IT news, programming updates, and technology trends directly on your phone.
About the author
Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
8 people found this page helpful