Search

Resume and bio of the author Couple of articles related to my hobby - robotics
My impressions about immigration to Australia. In Russian only. Recipes for everyday problems solving
Linux-related posts Windows-related posts
Software-related posts A lot of info about hardware
Different tools you might find useful Posts about various web technologies
Everything that doesn't fit the rest of the menu RSS feed for this blog

Tag Cloud

Archives

Links

Andrey Mikhalchuk’s Blog

Technoblog about life

Feb 4, 2008 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.

This post is published in How To, Linux, Web.

3 Responses to “Why *nix md5sum and PHP md5() do not match?”

  1. J Says:

    echo “eee” > /tmp/e

    That is the line that would have added the new line to the end of the file.

    try `echo -n` instead

  2. Andrey Mikhalchuk Says:

    > try `echo -n` instead
    This is exactly what the post suggests. What’s your point?

  3. jussi Says:

    Thanks for this!
    Invisible characters (ie. newline) on CLI are very difficult to spot.

Leave a Reply

You must be logged in to post a comment.

Site Map (c) Andrey Mikhalchuk, 2005-2008