[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

md5 function in Ruby

Denys Yakhnenko

12/5/2006 9:35:00 PM

Hello all:

I've been having trouble with Ruby's md5 function... The following code
snippet (file attached):

***begin code***
require 'digest/md5'

md5=Digest::MD5.hexdigest(File.read("C:\\dc.log"))
puts md5
***end code***

produces completely different hash than any of the other tools I use
(MD5Win32 or winMD5Sum)... Am I not using the function correctly? Wtf is
being hashed?

--
Posted via http://www.ruby-....

13 Answers

Denys Yakhnenko

12/5/2006 9:36:00 PM

0

Denys Yakhnenko wrote:
> Hello all:
>
> I've been having trouble with Ruby's md5 function... The following code
> snippet (file attached):
>

I get Application error (Rails) if I try to include the file as
attachment...

--
Posted via http://www.ruby-....

Denys Yakhnenko

12/5/2006 9:46:00 PM

0

Denys Yakhnenko wrote:
>
> I get Application error (Rails) if I try to include the file as
> attachment...

Ruby's output:

>C:\ruby\my>test2.rb
>68aad1424a2389dcd991b400a9b32934

MD5 tools output...
>8c149edff1d09df3a33871dc5c362906

I apologize for this many messages.

Denys


--
Posted via http://www.ruby-....

Urabe Shyouhei

12/5/2006 9:54:00 PM

0

Denys Yakhnenko wrote:
> produces completely different hash than any of the other tools I use
> (MD5Win32 or winMD5Sum)... Am I not using the function correctly? Wtf is
> being hashed?
>

You're on Windows? perhaps you are dancing with text mode...


Denys Yakhnenko

12/5/2006 9:57:00 PM

0

Urabe Shyouhei wrote:
>
> You're on Windows? perhaps you are dancing with text mode...

Yes I am on Windows. Can you please elaborate on that?

--
Posted via http://www.ruby-....

Austin Ziegler

12/5/2006 10:00:00 PM

0

On 12/5/06, Denys Yakhnenko <dyakhnenko@linkp.com> wrote:
> Hello all:
>
> I've been having trouble with Ruby's md5 function... The following code
> snippet (file attached):
>
> ***begin code***
> require 'digest/md5'
>
> md5=Digest::MD5.hexdigest(File.read("C:\\dc.log"))
> puts md5
> ***end code***

File.read is a bad choice on Windows. It should be avoided if at all
possible. Do this instead:

puts Digets::MD5.hexdigest(File.open("C:\\dc.log", "rb") { |f| f.read })

-austin
--
Austin Ziegler * halostatue@gmail.com * http://www.halo...
* austin@halostatue.ca * http://www.halo...feed/
* austin@zieglers.ca

Vincent Fourmond

12/5/2006 10:02:00 PM

0

Denys Yakhnenko wrote:
> ***begin code***
> require 'digest/md5'
>
> md5=Digest::MD5.hexdigest(File.read("C:\\dc.log"))
> puts md5
> ***end code***

For me, on a Linux box, with a random file, it works fine. Are you
sure your file is read properly ? See:


> 22:47 vincent@tanyaivinco ~ irb1.8
>> require 'digest/md5'
=> true
>> md5=Digest::MD5.hexdigest(File.read("qt-enum.patch"))
=> "c15a3cfc054d9af29b40a37805c27dbf"
>> %
22:48 vincent@tanyaivinco ~ md5sum qt-enum.patch
c15a3cfc054d9af29b40a37805c27dbf qt-enum.patch

Vince

--
Vincent Fourmond, PhD student
http://vincent.fourmon...

Ara.T.Howard

12/5/2006 10:04:00 PM

0

Mike Fletcher

12/5/2006 10:05:00 PM

0

Denys Yakhnenko wrote:
> Urabe Shyouhei wrote:
>>
>> You're on Windows? perhaps you are dancing with text mode...
>
> Yes I am on Windows. Can you please elaborate on that?

Windows has a notion of "text files" and "binary files"; for the former
translation is done of CR/NL characters, and no translation is done for
the later. The MD5 utilities you're using are putting their file
handles into binary mode and are seeing all of the actual octets of the
file's contents; you're seeing a different sequence of octets since your
file handle is in text mode.

See the IO#binmode method docs as well.


--
Posted via http://www.ruby-....

Denys Yakhnenko

12/5/2006 10:09:00 PM

0

Austin Ziegler wrote:
> On 12/5/06, Denys Yakhnenko <dyakhnenko@linkp.com> wrote:
>> ***end code***
> File.read is a bad choice on Windows. It should be avoided if at all
> possible. Do this instead:
>
> puts Digets::MD5.hexdigest(File.open("C:\\dc.log", "rb") { |f| f.read
> })
>
> -austin

Yup, that did it... thanks.


Thanks for replying all... those are some extremely useful hints.

--
Posted via http://www.ruby-....

Clifford Heath

12/5/2006 10:10:00 PM

0

Denys Yakhnenko wrote:
> md5=Digest::MD5.hexdigest(File.read("C:\\dc.log"))
> produces completely different hash than any of the other tools I use

Open the file in binary mode:

ruby -e 'require "digest/md5"; p Digest::MD5.hexdigest(File.read("C:\\dc.log", "rb"))'

Clifford Heath.