Why is my command-line hash different from online MD5 hash results?

22

1

On a Mac OS X v10.5 (Leopard) PowerPC, if I do:

echo "hello" | md5 
on the command line, the result is:
b1946ac92492d2347c6235b4d2611184

But if I enter hello into one of the online MD5 hash sites like http://md5online.net/, I get:

5d41402abc4b2a76b9719d911017c592

Am I doing something wrong? If I want to use MD5 on the go, how can I make sure what I'm getting on the command line will agree with the online md5 tools?

pellea72

Posted 2009-11-17T08:00:16.613

Reputation: 263

Works for me on Windows with Total Commander creating the MD5 checksum. Same as the online version. – Snark – 2009-11-17T08:04:18.510

Thanks, Snark. Rudedog solved the problem when using md5 on the command line, at least for POSIX systems. Give him a +1 if you can. I'm too new. – pellea72 – 2009-11-17T08:13:36.273

Answers

41

When you echo from the command line, md5 is calculating the sum of 6 characters - h,e,l,l,o plus newline. The text you enter in a website doesn't have a newline.

Try doing

echo -n hello | md5

and it'll give you what you expect. The -n tells echo not to output a newline.

Rudedog

Posted 2009-11-17T08:00:16.613

Reputation: 1 560

Oops. I didn't notice the '-n' tag. You're right Rudedog. That worked. Thanks. – pellea72 – 2009-11-17T08:11:17.060

3

You can also use printf instead of echo, which automatically suppresses the newline character:

printf hello | md5

Or even:

printf "hello" | md5

EarlGrey

Posted 2009-11-17T08:00:16.613

Reputation: 131

2

b1946ac92492d2347c6235b4d2611184 ist the md5 of just the string

hello

5d41402abc4b2a76b9719d911017c592 ist the md5 of

hello

CR+LF

CR+LF is the Windows newline.

user1863

Posted 2009-11-17T08:00:16.613

Reputation: 206

1

To add my 5 cents and extend the answer: in Linux is not md5, is md5sum, so you should be doing:

echo -n hello | md5sum

Simon Ernesto Cardenas Zarate

Posted 2009-11-17T08:00:16.613

Reputation: 139