MD5 digest doesn't match digest from md5sum in shell

I'm having problems not being able to get the MD5 digest from ruby to match the digest from the md5sum command in the bash shell

Digest::MD5.hexdigest("String")

is not producing the same hash as

echo "String" | md5sum

in the shell.

Am I missing something here?

Yes, the echo command adds a new line character to the end.

try:

echo -n "String" | md5sum

(smacks self in forehead...) Why is it always the obvious stuff that trips me up?

Thanks.

I'm having problems not being able to get the MD5 digest from ruby to match the digest from the md5sum command in the bash shell

Digest::MD5.hexdigest("String")

is not producing the same hash as

echo "String" | md5sum

in the shell.

Am I missing something here?

Well, perhaps that echo is sending a newline following your String and you don't put that into the literal that you pass in ruby.

echo is either the shell built-in or the one from the operating system. Check your docs or just try it out to see how to avoid the newline. md5 is the digest wrapper on MacOSX dumper is my own hex display tool. Use `od` or `cat -vet` at your option.

:Users/rab $ echo "String" | dumper -

  Record number 0:

0 5374 7269 6e67 0a |String.| :Users/rab $ echo -e "String\n\c" | dumper -

  Record number 0:

0 5374 7269 6e67 0a |String.|

:Users/rab $ echo "String" | md5 550ad2dc9ce7c5cff074cccdbfb690db :Users/rab $ echo -e "String\n\c" | md5 550ad2dc9ce7c5cff074cccdbfb690db :Users/rab $ irb

require 'digest/md5'

=> true

puts Digest::MD5.hexdigest("String\n")

550ad2dc9ce7c5cff074cccdbfb690db => nil

puts Digest::MD5.hexdigest("String")

27118326006d3829667a400ad23d5d98 => nil

exit

:Users/rab $ echo -e "String\c" | md5 27118326006d3829667a400ad23d5d98 :Users/rab $ echo -n "String" | md5 27118326006d3829667a400ad23d5d98 :Users/rab $ echo -n "String" | dumper -

  Record number 0:

0 5374 7269 6e67 |String| :Users/rab $ echo -e "String\c" | dumper -

  Record number 0:

0 5374 7269 6e67 |String|

It all looks good from here!

-Rob

Rob Biedenharn http://agileconsultingllc.com Rob@AgileConsultingLLC.com