PHP md5()Functions

PHP String 参考手册PHP String Reference Manual

Examples

Calculate the MD5 hash of the string "Hello":

<?php $str = "Hello"; 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 an input message of arbitrary length and converts it into a 128-bit "fingerprint" or "message digest" value that represents the input, and the converted value is used as the result. The MD5 algorithm is primarily designed for digital signature applications; in such applications, larger files are compressed in a secure manner before encryption (the encryption process is accomplished by setting a private key under a public key in a cryptosystem such as RSA).

To calculate the MD5 hash of a file, use themd5_file()function.

The md5() function cannot handle arrays; it returns null for arrays.md5(a[])The result isnull。

Online MD5 test tool:MD5 Online Encryption Tool


Syntax

md5(string,raw)

Parameter Description
string Required. Specifies the string to be calculated.
raw Optional. Specifies the hexadecimal or binary output format:
  • TRUE - Raw 16-character binary format
  • FALSE - Default. 32-character hexadecimal number

Technical Details

Return value: Returns the calculated MD5 hash on success, or FALSE on failure.
PHP Version: 4+
Changelog: In PHP 5.0,rawthe parameter became optional.


More Examples

Example 1

Output the result 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>"; ?>

Run example »

Example 2

Output the result of md5() and test it:

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

Run example »


PHP String 参考手册PHP String Reference Manual Other Extensions