Latest web development tutorials

PHP md5 () function

PHP String Reference PHP String Reference

Examples

Calculation string "Hello" MD5 hash:

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

Running instance »

Definition and Usage

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

md5 () function uses the RSA Data Security, including the MD5 message digest algorithm.

RFC 1321 interpretation from - MD5 message digestalgorithm:MD5message digest algorithm of arbitrary length message as input, and converts it into a 128-bit length of the "fingerprint" or "message digest" value to represent the enter a value, and the converted value as a result.MD5 algorithm is mainly for digital signature applications designed; in this digital signature applications, large files will be encrypted (encryption process here is through a password system under: public-key [such as RSA] under setting the private key and completed) in a secure manner prior to compression.

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


grammar

md5( string,raw )

参数 描述
string 必需。规定要计算的字符串。
raw 可选。规定十六进制或二进制输出格式:
  • TRUE - 原始 16 字符二进制格式
  • FALSE - 默认。32 字符十六进制数

technical details

return value: If successful calculated MD5 hash, if it fails it returns FALSE.
PHP version: 4+
Update log: In PHP 5.0 in, raw parameter becomes optional.


More examples

Example 1

Output md5 () result:

<?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>";
?>

Running instance »

Example 2

Results output md5 () of and test it:

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

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

Running instance »


PHP String Reference PHP String Reference