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_init() Function.
The built-in hash_init() function initializes an incremental hashcontext which can be used with other PHP hash functions such as hash_update(), hash_final() etc. It takes input as a hash algorithm and output as a hash context.
A hashcontext is generated based on the hash_algorithm used inside the hash_init(). You can update your data or message with the hashcontext using the hash_update() function and then get the final hash using the hash_final() function.
The built-in hash_init() function initializes an incremental hashcontext which can be used with other PHP hash functions such as hash_update(), hash_final() etc. It takes input as a hash algorithm and output as a hash context.
A hashcontext is generated based on the hash_algorithm used inside the hash_init(). You can update your data or message with the hashcontext using the hash_update() function and then get the final hash using the hash_final() function.
Syntax
Following below is the syntax to use this function -
hash_init ( string $algo [, int $options = 0 [, string $key = NULL ]] ) : HashContext
Parameter Details
Sr.No | Parameter & Description |
---|---|
1 | algo Name of the hashing algorithm. There is a big list of algorithm available with hash, some important ones are md5, sha256, etc. To get the full list of algorithms supported use the hashing function hash_algos() |
2 | options There is only one option supported and that is HASH_HMAC. If you are using options, the key is also mandatory. |
3 | key If HASH_HMAC is used as an option, the key also has to be given and it will be a shared secret key that will be used with HMAC hashing method. |
Return Value
It returns a hashing context. The hashing context can be used with other functions like the hash_update(), hash_update_file(), hash_final() and hash_update_stream().
PHP Version
This built-in PHP function works from PHP version greater than 5.1.2.
Example1
The following example illustrates how to generate a hashing context -
<?php $hash_context = hash_init('md5'); hash_update($hash_context, 'Testing php'); hash_update($hash_context, ' hash functions.'); echo hash_final($hash_context); ?>
Output
When the above code is executed, it will produce the following result -
e4310012c89a4b8479fd83694a2a3a31
Example2
The following example illustrates the usage of the built-in PHP hash_init() function with the hash_copy() -
<?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
Alright guys! This is where we are rounding up for this tutorial post. In our next tutorial, we are going to be studying about the PHP hash_update() 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 tutorials delivered directly to your emails.
Thanks for reading and bye for now.
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.