The PHP language comes with several functions to hash a string based on different algorithms like "sha1", "sha256", "md5" etc. These functions all take a string as an argument and output an Alpha-Numeric hashed string.
Syntax
string hash($algo, $string, $getRawOutput)
In this program, we are generating the hash code from a random value. For that first, we have to assign the random value to the variable str the random value is generated using the built-in function rand().
The to generate the hashing code by using the built-in function hash()
with 'sha256'(string-based algorithm for generating the hash code) and value of the variable str as the arguments of the function and assign the value into the variable hashedCode and after that, we can print the value of the variable hashedCode as the generated hash code.
Step 1: Assign a random value using the built-in function rand()
to the variable str
Step 2: Generate the hashing code by using the built-in function hash()
with 'sha256'(string-based algorithm for generating the hash code) and value of the variable str as the arguments of the function and assign the value into the variable hashedCode
Step 3: Print the value of the variable hashedCode as the generated hash code
<?php
$str = rand();
$hashedCode = hash("sha256", $str);
echo " The generated hash code is: $hashedCode";
?>
The generated hash code is: 6edbe95a6c08483d06d7c3dfff5259f3bd1c50e680916bcda5f37232363cacae