PHP | hash_copy() Function

PHP | hash_copy() 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_copy() Function.

The PHP hash_copy() function is used to copy the hashing context generated from hash_init().

Syntax

Following below is the syntax to use this function -

hash_copy ( HashContext $context ) : HashContext


Parameter Details

Sr.NoParameter & Description
1

HashContext context

The hash context that you get using hash_init().


Return Value

It returns a copy of hash context. The hash context can be used along with other hash function like hash_update_file(), hash_final(), hash_update() and hash_update_stream().

PHP Version

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

Example1

Working with the built-in PHP hash_copy() and hash_init() functions -

<?php
   $hash_context = hash_init("md5");
   hash_update($hash_context, "Welcome To Webdesigntutorialz");
   $hash_copy= hash_copy($hash_context);
   echo hash_final($hash_context);
   echo "<br/>";
   hash_update($hash_copy,  "Welcome To Webdesigntutorialz");
   echo hash_final($hash_copy);
?>

Output

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

380d3ca1b0ab972f207209c076895310<br/>621058f8d42e78586b987c7b5233dc77

Example2

Working with the hash_copy() and sha256

<?php
   $hash_context = hash_init("sha256");
   hash_update($hash_context, "Welcome To Webdesigntutorialz");
   $hash_copy = hash_copy($hash_context);
   hash_update($hash_copy,  "Welcome To Webdesigntutorialz");
   echo hash_final($hash_copy);
?>

Output

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

c37c7726bd39bab917ab8eb2143f914f9c37937bc48fb3b3ef11860867f93c20


Alright guys! This is where we are rounding up for this tutorial post. In our next tutorial, we will be studying about the hash_equals() Function.

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 tutorial posts delivered directly to your emails.

Thanks for reading and bye for now.