I recently tried to calculate md5 from *nix using md5sum and PHP md5() function. The result might surprise you – they don’t match in the situation you expect them to match. Here is explanation why and how to fix that.
First, let’s take a look at the following tests:
> php -r 'echo md5( "eee" );'
> echo "eee"|md5sum
> echo "eee" > /tmp/e
> md5sum /tmp/e
> vi /tmp/w
<put eee in the file>
> md5sum /tmp/w
> php -r 'echo md5( "eee\n" );'
So as you can see it looks like md5sum adds a newline to the end of every text you’re trying to get hash for. I also fall into this illusion but a little bit investigation shown that it’s not true. Here’s the proof:
eee<Hit Ctrl-D twice here>d2f2297d6e829cd3493aa7de4416a18f
The truth is that echo adds newline to the output, so you have to use -n flag:
> echo -n "eee"|md5sum
Also most editors add newline to the end of the file even if you don’t want that:
> od -a /tmp/w
0000000 e e e nl
See that last nl?
So both md5sum and md5() work fine, just be careful about the way you use it.
This post is published in How To, Linux, Web.