note 78549 deleted from function.md5 by danbrown
| From: | danbrown@php.net | Date: | Mon, 20 Jun 2011 15:16:34 +0000 |
| Subject: | note 78549 deleted from function.md5 by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-180728@lists.php.net to get a copy of this message | ||
Note Submitter: Santa Cruz skelly
----
Re: public at kreinacke dot com
It has long been a point of frustration to me that md5() functions don't produce the same
results as the *nix md5sum program. Reading your comments about the incongruity today pushed me over
the edge to finally understand why. The answer, I believe I have found, is that md5sum includes an
extra newline as the termination of the file content. ugh.
In response I have produced the following function that, at least on my server, produces a result
matching the output of md5sum on the command line:
function md5sum($str) { return(md5($str . "\n")); }
Yes, that easy.
My results from typing the string "apples and oranges" into a PHP script using that
function and the same string entered into a file on disk using md5sum on the command line to pull it
return:
6c1111418017a050006eee9f2bdda2bf
Your
echo -n "apples and oranges | md5sum method (which suppresses that newline I
refer to!) and PHP's normal md5() function also return the same result as one another as you
described, but those are string based operations. My solution is a function that reproduces the
result of md5sum's assessment of on-disk data based on the contents of the file being in a
string, something that's critical to my application.