简体   繁体   中英

MD5 gives different result between PHP and ash/OSX

I'm creating a hash on Busybox that I'm comparing with in a php script.

However results are anything but desired:

BusyBox v1.15.3 echo A|md5sum     bf072e9119077b4e76437a93986787ef  -
MacBook-Air:~ $ echo A|md5        bf072e9119077b4e76437a93986787ef
MacBook-Air:~ $ echo "A"|md5      bf072e9119077b4e76437a93986787ef
[root@centos67 ~] echo "A"|md5sum bf072e9119077b4e76437a93986787ef
.
PHP LAMP: echo md5("A");          7fc56270e7a70fa81a5935b72eacbe29
PHP MAMP: echo md5("A");          7fc56270e7a70fa81a5935b72eacbe29

Additionally, googled online md5 generating scripts always return the same result as the PHP ones above.

Why is this happening? What is (if any) the difference between md5 and md5sum, and if there's a difference, why the naming inconsistency between busybox and OSX?

How can I get PHP to generate the same hash my Busybox generates? The other way around (make Busybox gerenate a PHP md5) is also ok, but it does have to work with my basic Busybox version.

Found out the answer was quite simple.

GNU echo always ends with a newline .

Two possible fixes:

BusyBox v1.15.3 echo -n A|md5sum     7fc56270e7a70fa81a5935b72eacbe29  -
MacBook-Air:~ $ echo -n A|md5        7fc56270e7a70fa81a5935b72eacbe29
MacBook-Air:~ $ echo -n "A"|md5      7fc56270e7a70fa81a5935b72eacbe29
[root@centos67 ~] echo -n "A"|md5sum 7fc56270e7a70fa81a5935b72eacbe29
.
PHP LAMP: echo md5("A");             7fc56270e7a70fa81a5935b72eacbe29
PHP MAMP: echo md5("A");             7fc56270e7a70fa81a5935b72eacbe29

or

BusyBox v1.15.3 echo A|md5sum        bf072e9119077b4e76437a93986787ef  -
MacBook-Air:~ $ echo A|md5           bf072e9119077b4e76437a93986787ef
MacBook-Air:~ $ echo "A"|md5         bf072e9119077b4e76437a93986787ef
[root@centos67 ~] echo "A"|md5sum    bf072e9119077b4e76437a93986787ef
.
PHP LAMP: echo md5("A\n");           bf072e9119077b4e76437a93986787ef
PHP MAMP: echo md5("A\n");           bf072e9119077b4e76437a93986787ef

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM