How to md5 a string?

B

Bontina Chen

I'm using hexdigest in Digest/MD5 but got totally different outputs from
other tools.
I got some clues that in windows I should read the source I would like
to hash with binary mode.
But I have no idea how to transform a string to binary mode.
Anyone could give my some suggestion?

Thx


ABon
 
C

Chris Shea

I'm using hexdigest in Digest/MD5 but got totally different outputs from
other tools.
I got some clues that in windows I should read the source I would like
to hash with binary mode.
But I have no idea how to transform a string to binary mode.
Anyone could give my some suggestion?

Thx

ABon

It might be informative to see some examples what you're getting with
Digest/MD5 vs other tools. I've used Digest/MD5 with great success on
Windows.

If what you're doing is getting the MD5 of a file, opening it for
reading in binary mode is done like so:

File.new('filename','rb')

Chris
 
C

Clifford Heath

Bontina said:
I'm using hexdigest in Digest/MD5 but got totally different outputs from
other tools.
I got some clues that in windows I should read the source I would like
to hash with binary mode.
But I have no idea how to transform a string to binary mode.
Anyone could give my some suggestion?

Try:

p my_string.split(//)

and see if you get the same sequence of bytes you expect other
tools to see. Also try "puts my_string.size" to verify the byte
count.
 
B

Bontina Chen

Chris said:
It might be informative to see some examples what you're getting with
Digest/MD5 vs other tools. I've used Digest/MD5 with great success on
Windows.

If what you're doing is getting the MD5 of a file, opening it for
reading in binary mode is done like so:

File.new('filename','rb')

Chris

s="http://pandora.com"
Digest::MD5.hexdigest(s)

Abon
 
C

Chris Shea

It might be informative to see some examples what you're getting with
Digest/MD5 vs other tools. I've used Digest/MD5 with great success on
Windows.
If what you're doing is getting the MD5 of a file, opening it for
reading in binary mode is done like so:

Chris

s="http://pandora.com"
Digest::MD5.hexdigest(s)

Abon

C:\>ruby -v
ruby 1.8.6 (2007-03-13 patchlevel 0) [i386-mswin32]
C:\>irb
irb(main):001:0> require 'digest/md5'
=> true
irb(main):002:0> s = "http://pandora.com"
=> "http://pandora.com"
irb(main):003:0> Digest::MD5.hexdigest(s)
=> "9e565e1adf52310555fa2dc1391ded64"

--

mvb:~ cms$ ruby -v
ruby 1.8.6 (2007-03-13 patchlevel 0) [i686-darwin8.9.1]
mvb:~ cms$ irb
irb(main):001:0> require 'digest/md5'
=> true
irb(main):002:0> s = "http://pandora.com"
=> "http://pandora.com"
irb(main):003:0> Digest::MD5.hexdigest(s)
=> "9e565e1adf52310555fa2dc1391ded64"

--

I get the same result from http://www.xs4all.nl/~jlpoutre/BoT/Javascript/Utils/md5_hashing.html

Can you give a more detailed description of what you're experiencing?

Chris
 
C

Chris Shea

C:\>ruby -v
ruby 1.8.6 (2007-03-13 patchlevel 0) [i386-mswin32]
C:\>irb
irb(main):001:0> require 'digest/md5'
=> true
irb(main):002:0> s = "http://pandora.com"
=> "http://pandora.com"
irb(main):003:0> Digest::MD5.hexdigest(s)
=> "9e565e1adf52310555fa2dc1391ded64"

--

mvb:~ cms$ ruby -v
ruby 1.8.6 (2007-03-13 patchlevel 0) [i686-darwin8.9.1]
mvb:~ cms$ irb
irb(main):001:0> require 'digest/md5'
=> true
irb(main):002:0> s = "http://pandora.com"
=> "http://pandora.com"
irb(main):003:0> Digest::MD5.hexdigest(s)
=> "9e565e1adf52310555fa2dc1391ded64"

--

I get the same result fromhttp://www.xs4all.nl/~jlpoutre/BoT/Javascript/Utils/md5_hashing.html

Can you give a more detailed description of what you're experiencing?

Chris

Oh hell, while I'm at it:

~$ ruby -v
ruby 1.8.4 (2005-12-24) [i486-linux]
~$ irb
irb(main):001:0> require 'digest/md5'
=> true
irb(main):002:0> s = "http://pandora.com"
=> "http://pandora.com"
irb(main):003:0> Digest::MD5.hexdigest(s)
=> "9e565e1adf52310555fa2dc1391ded64"

--

chris[~]$ ruby -v
ruby 1.8.5 (2006-12-25 patchlevel 12) [i686-linux]
chris[~]$ irb
irb(main):001:0> require 'digest/md5'
=> true
irb(main):002:0> s = "http://pandora.com"
=> "http://pandora.com"
irb(main):003:0> Digest::MD5.hexdigest(s)
=> "9e565e1adf52310555fa2dc1391ded64"
 
B

Brian Candler

I'm using hexdigest in Digest/MD5 but got totally different outputs from
other tools.
I got some clues that in windows I should read the source I would like
to hash with binary mode.
But I have no idea how to transform a string to binary mode.
Anyone could give my some suggestion?

Your sources must be byte-for-byte identical, including any newlines.

e.g.

echo "hello" | md5sum
echo -n "hello" | md5sum

will give different answers.
 

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

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,241
Latest member
Lisa1997

Latest Threads

Top