md5 function in Ruby

  • Thread starter Denys Yakhnenko
  • Start date
D

Denys Yakhnenko

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?
 
D

Denys Yakhnenko

Denys said:
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...
 
U

Urabe Shyouhei

Denys said:
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...
 
A

Austin Ziegler

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
 
V

Vincent Fourmond

Denys said:
***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
22:48 vincent@tanyaivinco ~ md5sum qt-enum.patch
c15a3cfc054d9af29b40a37805c27dbf qt-enum.patch

Vince
 
A

ara.t.howard

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?

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
 
M

Mike Fletcher

Denys said:
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.
 
D

Denys Yakhnenko

Austin said:
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.
 
C

Clifford Heath

Denys said:
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.
 
B

Bontina Chen

Clifford said:
Open the file in binary mode:

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

Clifford Heath.

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

Thanks


Abon
 
C

Clifford Heath

Bontina said:
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 Heath.
 
S

Stuart Clarke

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top