PHP md5() Function
Example
Calculate the MD5 hash of the string "Hello":
<?php
$str = "Hello";
echo md5($str);
?>
Definition and Usage
The md5() function calculates the MD5 hash of a string.
The md5() function uses the RSA Data Security, including the MD5 Message-Digest Algorithm.
Explanation from RFC 1321 - MD5 Message-Digest Algorithm: The MD5 Message-Digest Algorithm takes an input and computes a 128-bit fingerprint or message digest that represents the input. The algorithm is designed for digital signature applications, where a large file is securely compressed before being encrypted in a public key system under a cryptographic scheme such as RSA.
To calculate the MD5 hash of a file, use the md5_file() function.
The md5() function cannot handle arrays; arrays return null, md5(a[]) results in null.
Syntax
| Parameter | Description |
|---|---|
| string | Required. The string to be calculated. |
| raw | Optional. Specifies the output format: TRUE - Raw 16 character binary format<br> FALSE - Default. 32 character hexadecimal number |
Technical Details
| Return Value: | Returns the computed MD5 hash on success, FALSE on failure. |
|---|---|
| PHP Version: | 4+ |
| --- | --- |
| Changelog: | The raw parameter became optional in PHP 5.0. |
| --- | --- |
More Examples
Example 1
Output the results of md5():
<?php
$str = "Hello";
echo "The string: ".$str."<br>";
echo "TRUE - Raw 16 character binary format: ".md5($str, TRUE)."<br>";
echo "FALSE - 32 character hex number: ".md5($str)."<br>";
?>
Example 2
Output the results of md5() and test them:
<?php
$str = "Hello";
echo md5($str);
if (md5($str) == "8b1a9953c4611296a827abf8c47804d7")
{
echo "<br>Hello world!";
exit;
}
?>