• PHP Video Tutorials

PHP - Hash update stream() Function



Definition and Usage

The hash_update_stream() function will update the hash context from an open stream.

Syntax

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

Parameters

Sr.No Parameter & 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 Values

PHP hash_update_stream() function returns number of bytes that are used by the hashing context from the handle.

PHP Version

This function will work from PHP Version greater than 5.1.2.

Example 1

Using hash_update_stream() −

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

Output

This will produce the following result −

6211420491a571f89f970683221d4480

Example 2

Using hash_update_stream() with gost-crypto algorithm −

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

Output

This will produce the following result −

9d9c7fb112d23e3d3c68ec1cb1f8c292d1b14d01cc26c302907c07bd487cddb2

Example 3

Using hash_update_stream() using fopen() −

<?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

This will produce following result −

61dbcac417fbb43e97c33b0f3eb86d6733712beaa1ec9c8084aa6063667c7602
php_function_reference.htm
Advertisements