PHP md5 is an inbuilt function in PHP. It returns the MD5 Hash of the string after hashing it with the RSA Data Security, Inc. MD5 Message-Digest Algorithm. Hashing a string is very important to store critical data in a database like for storing user’s passwords. In this article, we will discuss the PHP md5 Function. Also, we will discuss a couple of examples of using it.
WARNING: It is not recommended to use this function to secure passwords, due to the fast nature of this hashing algorithm. See the Password Hashing FAQ for details and best practices.https://php.net/manual/en/function.md5.php
string md5 ($string, $getRawOutput)
The function expects two parameters. However, only one parameter is mandatory while the other is optional.
- $string: The string which needs to be hashed is the first parameter. Also, it is a mandatory parameter.
- $getRawOutput: The second parameter is a boolean value with default as false. By default the function returns a 32 character hexadecimal sequence. However, on passing true to the function, it returns a raw binary string of length 16.
The PHP md5 Function returns string after hashing it using the RSA Data Security, Inc. MD5 Message-Digest Algorithm. By default, the function returns the string as a 32 character hexadecimal sequence. It can also return a string raw binary string of 16 characters.
Let’s discuss a few examples of using the function.
Example 1: 32 Character Hexadecimal Character Sequence
For instance, consider an example of the function returning a 32 character hexadecimal character sequence.
<?php $string = 'Concatly'; $hashedString = md5($string); echo $hashedString; ?>
In the above example, the function returns the md5 hash of the string as follows:
Example 2: 16 Character Raw Binary
Also, on passing the second parameter as true, the function will return a 16 character raw binary string.
<?php $string = 'Concatly'; $hashedString = md5($string, true); echo $hashedString; ?>
Uses of PHP md5 Function
The authentication system is one of the most crucial component of a website. It is very important to remove vulnerabilities which an attacker may exploit. One such vulnerability is storing the user’s password in plain text form. If an unauthorised person gains access to the database, the whole system will get compromised.
Password hashing is a method in which the user’s password is hashed to a fixed length and stored in the database in an encrypted form. Even if someone gets to know the hashed password, he cannot compromise the whole system. PHP md5 is one of the function which is helpful for the same.
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.