PHP | hash_update_stream() Function

PHP hash_update_stream() Function


Hello folks! welcome back to a new edition of our tutorial on PHP. In this tutorial guide, we are going to be studying about the PHP hash_update_stream() Function.

The built-in hash_update_stream() function in PHP updates the hash context from an open stream.

Syntax

Following below is the syntax to use this function -

hash_update_stream ( HashContext $context , resource $handle [, int $length = -1 ] ) : int


Parameter Details

Sr.NoParameter & Description
1

HashContext context

The hash context that you get using hash_init().

2

handle

The file handler returned by the stream creation function.

3

length

The max characters to take from the handle into the hashing context.


Return Value

This built-in function returns the number of bytes that are used by the hashing context from the handle.

PHP Version

This built-in PHP function works from PHP version greater than 5.1.2.

Example1

The following below is an example which demonstrates the usage of the built-in PHP hash_update_stream() function -

<?php
   $file = tmpfile();
   fwrite($file , 'Welcome To Webdesigntutorialz');
   rewind($file);
   $hash_context = hash_init('md5');
   hash_update_stream($hash_context, $file);
   echo hash_final($hash_context);
?>

Output

When the above code is executed, it will produce the following result -

380d3ca1b0ab972f207209c076895310

Example2

Using hash_update_stream() with gost-crypto algorithm -

<?php
   $file = tmpfile();
   fwrite($file , 'Welcome To Webdesigntutorialz');
   rewind($file);
   $hash_context = hash_init('gost-crypto');
   hash_update_stream($hash_context, $file);
   echo hash_final($hash_context);
?>

Output

When the above code is executed, it will produce the following result -

88ce827b73674dfb1696a7d93651eba74d0b4b6fb42a0dbaaff0a45c28926d88

Example3

Using hash_update_stream() with fopen() function -

<?php
   $stream = fopen('a.txt', 'r');
   rewind($stream);
   $hash_context = hash_init('gost-crypto');
   hash_update_stream($hash_context, $stream);
   echo hash_final($hash_context);
?>

Output

When the above code is executed, it will produce the following result -

61dbcac417fbb43e97c33b0f3eb86d6733712beaa1ec9c8084aa6063667c7602


Alright guys! This is where we are rounding up for this tutorial post. In our next tutorial, we will be studying about the PHP MYSQLi Functions.

Do feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Do follow us on our various social media handles available and also subscribe to our newsletter to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.