PHP 8.3.14 Released!

md5

(PHP 4, PHP 5, PHP 7, PHP 8)

md5计算字符串的 MD5 散列值

警告

由于此函数依赖的算法已不足够复杂,不推荐使用此函数对明文密码加密。详细内容参见 这里

说明

md5(string $string, bool $binary = false): string

使用 » RSA 数据安全公司的 MD5 消息摘要算法 计算 string 的 MD5 散列值,并返回该散列值。

参数

string

要计算的字符串。

binary

如果可选的 binary 被设置为 true,那么 md5 摘要将以 16 字符长度的原始二进制格式返回。

返回值

以 32 字符的十六进制数形式返回散列值。

示例

示例 #1 md5() 示例

<?php
$str
= 'apple';

if (
md5($str) === '1f3870be274f6c49b3e31a0c6728957f') {
echo
"Would you like a green or red apple?";
}
?>

参见

添加备注

用户贡献的备注 2 notes

up
15
yiminrong at yahoo dot ca
3 years ago
Regarding Ray Paseur's comment, the strings hash to:

0e462097431906509019562988736854
0e830400451993494058024219903391

The odds of getting a hash exactly matching the format /^0+e[0-9]+$/ are not high but are also not negligible.

It should be added as a general warning for all hash functions to always use the triple equals === for comparison.

Actually, the warning should be in the operators section when comparing string values! There are lots of warnings about string comparisons, but nothing specific about the format /^0+e[0-9]+$/.
up
5
Ray.Paseur sometimes uses Gmail
5 years ago
md5('240610708') == md5('QNKCDZO')

This comparison is true because both md5() hashes start '0e' so PHP type juggling understands these strings to be scientific notation. By definition, zero raised to any power is zero.
To Top