Why *nix md5sum and PHP md5() do not match?

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" );'
d2f2297d6e829cd3493aa7de4416a18f


> echo "eee"|md5sum
5536fc9343e9ab0aca071354084dd3cc -


> echo "eee" > /tmp/e
> md5sum /tmp/e
5536fc9343e9ab0aca071354084dd3cc /tmp/e


> vi /tmp/w
<put eee in the file>
> md5sum /tmp/w
5536fc9343e9ab0aca071354084dd3cc /tmp/w


> php -r 'echo md5( "eee\n" );'
5536fc9343e9ab0aca071354084dd3cc

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:


> md5sum
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
d2f2297d6e829cd3493aa7de4416a18f -

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
0000004

See that last nl?
So both md5sum and md5() work fine, just be careful about the way you use it.

3 thoughts on “Why *nix md5sum and PHP md5() do not match?

Leave a Reply