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

>
>
> 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
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 Biedenharn          http://agileconsultingllc.com
[email protected]



--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to