29

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
  • 333

5 Answers5

55

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
  • 1,880
6

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

printf hello | md5

Or even:

printf "hello" | md5
EarlGrey
  • 161
1

b1946ac92492d2347c6235b4d2611184 ist the md5 of just the string

hello

5d41402abc4b2a76b9719d911017c592 ist the md5 of

hello

CR+LF

CR+LF is the Windows newline.

user1863
  • 214
0

Depends on how you call (OSx), bash and zsh add newline character by default.

echo "123" | md5

ba1f2511fc30423bdbb183fe33f3dd0f


echo "123" | tr -d "\n" | md5        

202cb962ac59075b964b07152d234b70

echo -n "123" | md5           

202cb962ac59075b964b07152d234b70

printf "123" | md5

202cb962ac59075b964b07152d234b70

0
  • The <<< redirection operator in bash also appends newline.
  • Both the | and <<< too can be analysed/diagnosed with od -c utility.
  • So: echo "hello" | od -c and od -c <<< "hello"; you will see a trailing newline in both these cases.

More details posted at this answer by me: https://stackoverflow.com/a/78654998/8395964