PHP Function Reference

PHP md5() Function



The PHP md5() function calculates the MD5 hash of string using the RSA Data Security, Inc. MD5 Message-Digest Algorithm, and returns that hash.

From RFC 1321: The MD5 message-digest algorithm takes as input a message of arbitrary length and produces as output a 128-bit "fingerprint" or "message digest" of the input. The MD5 algorithm is intended for digital signature applications, where a large file must be "compressed" in a secure manner before being encrypted with a private (secret) key under a public-key cryptosystem such as RSA.

Note: It is not recommended to use this function to secure passwords, due to the fast nature of this hashing algorithm.

Syntax

md5(string, binary)

Parameters

string Required. Specify the input string.
binary Optional. If set to true, then the MD5 digest is returned in raw binary format with a length of 16, otherwise the returned value is a 32-character hexadecimal number. Default is false.

Return Value

Returns the MD5 hash as a string.

Example:

The example below illustrates on md5() function.

<?php
$str = "Hello";

echo "The string is: $str \n";
echo "The hash of string is: \n";
echo md5($str);
?>

The output of the above code will be:

The string is: Hello 
The hash of string is: 
8b1a9953c4611296a827abf8c47804d7

Example:

Consider one more example where optional parameter is used to calculate the hash of the string in different formats.

<?php
$str = "Hello";

echo "16 character binary format:\n";
echo md5($str, true);
echo "\n32 character hexadecimal number: \n";
echo md5($str);
?>

The output of the above code will be:

16 character binary format:
‹™SÄa–¨'«øÄx×
32 character hexadecimal number: 
8b1a9953c4611296a827abf8c47804d7

❮ PHP String Reference