PHP md5() function

Example

Calculate the MD5 hash of the string "Hello":

<?php
$str = "Shanghai";
echo md5($str);
?>

Run Example

Definition and Usage

The md5() function calculates the MD5 hash of a string.

The md5() function uses RSA data security, including the MD5 message digest algorithm.

Explanation from RFC 1321 - MD5 Message Digest Algorithm: The MD5 Message Digest Algorithm takes any length of information as input and converts it into a 128-bit length "fingerprint information" or "message digest" value to represent this input, and uses the converted value as the result. The MD5 algorithm is mainly designed for digital signature applications; in these digital signature applications, larger files are compressed in a secure manner before encryption (here, the encryption process is completed by setting the private key under a public key in a cryptographic system [such as RSA]).

To calculate the MD5 hash of a file, please use md5_file() Function.

Syntax

md5(string,raw)
Parameter Description
string Required. Specify the string to be calculated.
raw

Optional. Specify the hexadecimal or binary output format:

  • TRUE - Original 16-character binary format
  • FALSE - Default. 32-character hexadecimal number

Technical Details

Return Value: Returns the calculated MD5 hash if successful, or FALSE if unsuccessful.
PHP Version: 4+
Update Log: In PHP 5.0,raw The parameter has become optional.

More Examples

Example 1

Output the result of md5():

<?php
$str = "Shanghai";
echo "String: " . $str . "<br>";
echo "TRUE - Original 16-character binary format: " . md5($str, TRUE) . "<br>";
echo "FALSE - 32-character hexadecimal format: " . md5($str) . "<br>";
?>

Run Example

Example 2

Output the result of md5() and then test it:

<?php
$str = "Shanghai";
echo md5($str);
if (md5($str) == "5466ee572bcbc75830d044e66ab429bc")
  {
  echo "<br>Hello world!";
  exit;
  }
?>

Run Example