百科狗-知识改变命运!
--

md5() - 计算字符串的 MD5 散列值 - php 字符串函数

梵高1年前 (2023-11-21)阅读数 19#技术干货
文章标签字符串

md5()

(PHP 4, PHP 5, PHP 7)

计算字符串的 MD5 散列值

Warning

It is not recommended to use this function to secure passwords, due to the fast nature of this hashing algorithm. See thePassword Hashing FAQfor details and best practices.

说明

md5(string $str[,bool $raw_output= FALSE] ): string

使用» RSA 数据安全公司的 MD5 报文算法计算$str的 MD5 散列值。

参数

$str

原始字符串。

$raw_output

如果可选的$raw_output被设置为TRUE,那么 MD5 报文摘要将以16字节长度的原始二进制格式返回。

返回值

md5() - 计算字符串的 MD5 散列值 - php 字符串函数

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

范例

Example #1md5()范例

参见

  • md5_file()计算指定文件的 MD5 散列值
  • sha1_file()计算文件的 sha1 散列值
  • crc32()计算一个字符串的 crc32 多项式
  • sha1()计算字符串的 sha1 散列值
  • hash()生成哈希值(消息摘要)
  • crypt()单向字符串散列
  • password_hash()创建密码的散列(hash)
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.
Use the strict comparision/identity operator:
php > var_dump(md5('240610708') == md5('QNKCDZO'));
php shell code:1:
bool(true)
php > var_dump(md5('240610708') === md5('QNKCDZO'));
php shell code:1:
bool(false)
php >
because, as mentioned previously, using the equality operator may result in false positives.
The complement of hey2raw.
You can use to convert from raw md5-format to human-readable format.
This can be usefull to check "Content-Md5" HTTP-Header.
From the documentation on Digest::MD5:
md5($data,...)
This function will concatenate all arguments, calculate the MD5 digest of this "message", and return it in binary form.
md5_hex($data,...)
Same as md5(), but will return the digest in hexadecimal form. 
PHP's function returns the digest in hexadecimal form, so my guess is that you're using md5() instead of md5_hex(). I have verified that md5_hex() generates the same string as PHP's md5() function.
(original comment snipped in various places)
>Hexidecimal hashes generated with Perl's Digest::MD5 module WILL
>NOT equal hashes generated with php's md5() function if the input
>text contains any non-alphanumeric characters.
>
>$phphash = md5('pa$$');
>echo "php original hash from text: $phphash";
>echo "md5 hash from perl: " . $myrow['password'];
>
>outputs:
>
>php original hash from text: 0aed5d740d7fab4201e885019a36eace
>hash from perl: c18c9c57cb3658a50de06491a70b75cd
Note: Before you get some idea like using md5 with password as way to prevent others tampering with message, read pages "Length extension attack" and "Hash-based message authentication code" on wikipedia. In short, naive constructions can be dangerously insecure. Use hash_hmac if available or reimplement HMAC properly without shortcuts.
Do not use the hex strings returned by md5() as a key for MCrypt 256-bit encryption. Hex characters only represent four bits each, so when you take 32 hex characters, you are only really using a 128-bit key, not a 256-bit one. 
Using an alphanumeric key generator [A-Za-z0-9] will also only provide a 192-bit key in 32 characters.
Two different MD5s concatenated in raw binary form, or mcrypt_create_iv(32,MCRYPT_DEV_RANDOM) will give you a true 256-bit key string.
Sometimes it's useful to get the actual, binary, md5 digest. 
You can use this function for it:
If you want to hash a large amount of data you can use the hash_init/hash_update/hash_final functions.
This allows you to hash chunks/parts/incremental or whatever you like to call it.

鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com

免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)

图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)