Why PHP's md5 is different from OpenSSL's md5?

女生的网名这么多〃 提交于 2019-12-04 03:14:52

问题


I am quite confused as to why I am seeing different results for md5 hashing in PHP and in OpenSSL.

Here is the code that I am running:

php -r "echo md5('abc');"

Results in: 900150983cd24fb0d6963f7d28e17f72

While this:

echo abc | openssl md5

Results in: 0bee89b07a248e27c83fc3d5951213c1

Why?


回答1:


There is only one way to compute MD5.

A blind guess is that the second one also includes a newline inside the string being hashed.

Yeh, verified it. That's it.




回答2:


As everyone noted, the problem is that echo prints an extra newline.

However, the solution proposed (echo -n) is not completely correct. According to the POSIX standard, "Implementations shall not support any options." You'll make the world a bit better if you don't use it. Use

printf %s abc | openssl md5

or simply

printf abc | openssl md5



回答3:


echo normally adds a new line character at the end of the string it outputs; that is the reason the MD5 values are different.

Try with echo -n abc | openssl md5.




回答4:


As jdehaan notes, if you tell echo not output a newline, you get the answer you expect

echo -n "abc" | openssl md5
900150983cd24fb0d6963f7d28e17f72


来源:https://stackoverflow.com/questions/3169605/why-phps-md5-is-different-from-openssls-md5

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!