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?

On Aug 21, 2009, at 2:45 PM, [email protected] wrote:

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
irb> require ‘digest/md5’
=> true
irb> puts Digest::MD5.hexdigest(“String\n”)
550ad2dc9ce7c5cff074cccdbfb690db
=> nil
irb> puts Digest::MD5.hexdigest(“String”)
27118326006d3829667a400ad23d5d98
=> nil
irb> 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 B. http://agileconsultingllc.com
[email protected]

On Fri, Aug 21, 2009 at 11:45 AM, [email protected][email protected]
wrote:

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


Aaron T.
http://synfin.net/
http://tcpreplay.synfin.net/ - Pcap editing and replay tools for Unix &
Windows
Those who would give up essential Liberty, to purchase a little
temporary
Safety, deserve neither Liberty nor Safety.
– Benjamin Franklin

On Aug 21, 2:44 pm, Aaron T. [email protected] wrote:

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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs