[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

How to md5 a string?

Bontina Chen

3/30/2007 3:13:00 AM


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

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

6 Answers

Chris Shea

3/30/2007 3:41:00 AM

0

On Mar 29, 9:13 pm, Bontina Chen <abonc...@gmail.com> wrote:
> 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
>
> --
> Posted viahttp://www.ruby-....

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

Clifford Heath

3/30/2007 4:19:00 AM

0

Bontina Chen wrote:
> 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.

Bontina Chen

3/30/2007 5:06:00 AM

0

Chris Shea wrote:
> On Mar 29, 9:13 pm, Bontina Chen <abonc...@gmail.com> wrote:
>>
>> --
>> Posted viahttp://www.ruby-....
>
> 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...
Digest::MD5.hexdigest(s)

Abon

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

Chris Shea

3/30/2007 5:18:00 AM

0

On Mar 29, 11:06 pm, Bontina Chen <abonc...@gmail.com> wrote:
> Chris Shea wrote:
> > On Mar 29, 9:13 pm, Bontina Chen <abonc...@gmail.com> wrote:
>
> >> --
> >> Posted viahttp://www.ruby-....
>
> > 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...
> Digest::MD5.hexdigest(s)
>
> Abon
>
> --
> Posted viahttp://www.ruby-....

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...
=> "http://pandora...
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...
=> "http://pandora...
irb(main):003:0> Digest::MD5.hexdigest(s)
=> "9e565e1adf52310555fa2dc1391ded64"

--

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

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

Chris

Chris Shea

3/30/2007 5:24:00 AM

0

On Mar 29, 11:18 pm, "Chris Shea" <cms...@gmail.com> wrote:
> On Mar 29, 11:06 pm, Bontina Chen <abonc...@gmail.com> wrote:
>
>
>
> > Chris Shea wrote:
> > > On Mar 29, 9:13 pm, Bontina Chen <abonc...@gmail.com> wrote:
>
> > >> --
> > >> Posted viahttp://www.ruby-....
>
> > > 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...
> > Digest::MD5.hexdigest(s)
>
> > Abon
>
> > --
> > Posted viahttp://www.ruby-....
>
> 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...
> => "http://pandora...
> 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...
> => "http://pandora...
> irb(main):003:0> Digest::MD5.hexdigest(s)
> => "9e565e1adf52310555fa2dc1391ded64"
>
> --
>
> I get the same result fromhttp://www.xs4all.nl/~jlpoutre/BoT/Javascript/Utils/md5_ha...
>
> 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...
=> "http://pandora...
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...
=> "http://pandora...
irb(main):003:0> Digest::MD5.hexdigest(s)
=> "9e565e1adf52310555fa2dc1391ded64"

Brian Candler

3/30/2007 6:02:00 AM

0

On Fri, Mar 30, 2007 at 12:13:14PM +0900, Bontina Chen wrote:
>
> 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.