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.
hash_init ( string $algo [, int $options = 0 [, string $key = NULL ]] ) : HashContext
|Sr.No||Parameter & Description|
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()
There is only one option supported and that is HASH_HMAC. If you are using options, the key is also mandatory.
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.
<?php $hash_context = hash_init('md5'); hash_update($hash_context, 'Testing php'); hash_update($hash_context, ' hash functions.'); echo hash_final($hash_context); ?>
<?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); ?>
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.