PHP hash is an inbuilt function in PHP. It generates a hash value of the input string using the algorithm of your choice. In this article, we will discuss the PHP hash Function. Also, we will discuss a few examples of using it.
string hash($algo, $string, $getRawOutput = false)
The function expects three parameters. However, two parameters are mandatory and one is optional. The description of the parameters is as follows:
- $algo: The first parameter refers to the hashing algorithm to use. Some commonly used algorithms are sha1, sha256, md5, etc.
- $string: The string which needs to be hashed is the second parameter. Also, it is a mandatory parameter.
- $getRawOutput: The third parameter is a boolean value with default as false. If true, the function returns the hash in raw binary format.
The function returns the hash string either in lowercase hexadecimal characters or raw binary format as per the third parameter.
Let’s discuss a few examples of using the function.
Example 1: Generating sha1 Hash
<?php $string = 'Concatly'; $hashedString = hash('sha1', $string); echo $hashedString; ?>
Also, you can use the PHP sha1 Function to generate sha1 hash.
Example 2: Generating md5 Hash
Similarly, you can generate md5 hash using this function
<?php $string = 'Concatly'; $hashedString = hash('md5', $string); echo $hashedString; ?>
Also, you can use the PHP md5 Function to generate md5 hash.
Example 3: Raw Binary Format
By passing the third parameter as true, you can get the return hash in raw binary format.
<?php $string = 'Concatly'; $hashedString = hash('md5', $string, true); echo $hashedString; ?>
Vishesh is currently working as a Lead Software Engineer at Naukri.com. He passed out of Delhi College of Engineering in 2016 and likes to play Foosball. He loves traveling and is an exercise freak. His expertise includes Java, PHP, Python, Databases, Design and Architecture.