Md5 function in Ruby

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?

Denys Y. 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…

Denys Y. 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

Denys Y. 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…

Urabe S. wrote:

You’re on Windows? perhaps you are dancing with text mode…

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

On 12/5/06, Denys Y. [email protected] 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

Denys Y. wrote:

Urabe S. 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.

Denys Y. 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 [email protected] ~ irb1.8

require ‘digest/md5’
=> true

md5=Digest::MD5.hexdigest(File.read(“qt-enum.patch”))
=> “c15a3cfc054d9af29b40a37805c27dbf”

%
22:48 [email protected] ~ md5sum qt-enum.patch
c15a3cfc054d9af29b40a37805c27dbf qt-enum.patch

Vince

On Wed, 6 Dec 2006, Denys Y. wrote:

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?

maybe only part of your data ??

try

require ‘digest/md5’

buf = open(“C:\dc.log”,“rb”){|f| f.read}
md5 = Digest::MD5.hexdiges buf
puts md5

-a

Austin Z. wrote:

On 12/5/06, Denys Y. [email protected] 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.

Denys Y. 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 H…

Bontina C. wrote:

Clifford H. wrote:

ruby -e ‘require “digest/md5”; p
Digest::MD5.hexdigest(File.read(“C:\dc.log”, “rb”))’
I’m transforming a string.
How could I change a string to binary mode?

You don’t need to, it’s always a simple binary sequence.
Whether it got constructed correctly in the first place
is another thing though.

What’s with this thread being revived after nearly 4 months?

Clifford H…

Can i just confirm whats going on here. You are loading the contents of
the file into memory and obtaining an MD5 checksum of the data in
memory? What is the difference between file.open and file.read also what
does the , “rb” do?

Many thanks

Austin Z. wrote:

On 12/5/06, Denys Y. [email protected] 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

Clifford H. wrote:

Denys Y. 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 H…

I’m transforming a string.
How could I change a string to binary mode?

Thanks

Abon