Ruby and MD5 playing poorly together

20 views
Skip to first unread message

Ryan Waldron

unread,
May 9, 2009, 3:36:25 PM5/9/09
to hs...@googlegroups.com
OK, here's the deal:

Mac:~> ruby -v
ruby 1.8.6 (2009-03-31 patchlevel 368) [i686-darwin9.6.0]

Mac:~> irb -rubygems
irb(main):001:0> require 'md5'
=> true
irb(main):002:0> foo = MD5.md5("blahblahblah")
ArgumentError: wrong number of arguments (1 for 0)
        from /usr/local/lib/ruby/site_ruby/1.8/md5.rb:12:in `initialize'
        from /usr/local/lib/ruby/site_ruby/1.8/md5.rb:12:in `new'
        from /usr/local/lib/ruby/site_ruby/1.8/md5.rb:12:in `md5'
        from (irb):2
irb(main):003:0>

Naturally, this is wreaking havoc with the Adhearsion stuff I'm trying (working through the 'Getting started' stuff at http://adhearsion.com/getting_started), which uses this to build its login auth hash (in components/sandbox/sandbox.rb in my sample app):

identifying_hash = MD5.md5(username + ":" + password).to_s

The docs certainly indicate that calling MD5.md5 with a string is supposed to work.

Anybody have ideas what I've broken and how to fix it?

Bryan Powell

unread,
May 9, 2009, 4:06:07 PM5/9/09
to hs...@googlegroups.com
Use the "hexdigest" method:

bryan-powells-macbook-pro-5:~ bryan$ irb
>> require 'md5'
=> true
>> foo = MD5.hexdigest("blahblahblah")
=> "46ea0d5b246d2841744c26f72a86fc29"
>> 

/b

Ryan Waldron

unread,
May 9, 2009, 4:10:03 PM5/9/09
to hs...@googlegroups.com
Well, couple of problems with that:

1) I'm just trying to use the Adhearsion code; they're the ones doing the MD5.md5 stuff.

2) I actually tried using MD5.hexdigest method and the Adhearsion sandbox rejected me (sniff!), so I don't know whether what #hexdigest returns is the same as what MD5.md5 was *supposed* to return.

Grrr...

Ryan Waldron

unread,
May 9, 2009, 4:13:33 PM5/9/09
to hs...@googlegroups.com
On Sat, May 9, 2009 at 3:10 PM, Ryan Waldron <ryan.w...@gmail.com> wrote:
Well, couple of problems with that:

1) I'm just trying to use the Adhearsion code; they're the ones doing the MD5.md5 stuff.

What I mean by this is that it's apparently working for a lot of other folks, and the docs say it should work, so even if I found a workaround, I wouldn't be happy with it.  I want to know why it's not working for me like it (1) should according to docs, and (2) seems to for lots of other folks.

Curious and pathological mix of curiosity and stubbornness, that's me.
 

PJ Davis

unread,
May 9, 2009, 4:15:49 PM5/9/09
to hs...@googlegroups.com
what version of Ruby are you using? I'm able to make it work 

>> require 'md5'
=> true
>> MD5.md5("i am so awesome")
=> #<MD5: 7675e1119b712fe57fd5a576ea4b99b9>


% ruby --version
ruby 1.8.6 (2008-08-11 patchlevel 287) [i686-darwin9.6.0]
Ruby Enterprise Edition 20090421



PJ Davis

unread,
May 9, 2009, 4:17:05 PM5/9/09
to hs...@googlegroups.com
oh, i'm dumb. i didn't read the first 2 lines of the first mail. Sorry about that.
On May 9, 2009, at 3:13 PM, Ryan Waldron wrote:

Ryan Waldron

unread,
May 9, 2009, 4:17:20 PM5/9/09
to hs...@googlegroups.com
Mac:~> ruby -v
ruby 1.8.6 (2009-03-31 patchlevel 368) [i686-darwin9.6.0]



Bryan Powell

unread,
May 9, 2009, 4:18:37 PM5/9/09
to hs...@googlegroups.com
The code you provided works for me:

bryan-powells-macbook-pro-5:~ bryan$ irb
>> require 'md5'
=> true
>> foo = MD5.md5("blahblahblah")
=> #<MD5: 46ea0d5b246d2841744c26f72a86fc29>
>> 

Does MD5.hexdigest work in irb?

Also, here's my Ruby version:

bryan-powells-macbook-pro-5:~ bryan$ ruby -v
ruby 1.8.6 (2008-03-03 patchlevel 114) [universal-darwin9.0]

Ryan Waldron

unread,
May 9, 2009, 4:19:42 PM5/9/09
to hs...@googlegroups.com
#hexdigest does work, and returns a hash string fine in both IRB and the Adhearsion app.  It just won't connect, and I don't know whether hexdigest is returning what MD5.md5 was supposed to.

Bryan Powell

unread,
May 9, 2009, 4:34:18 PM5/9/09
to hs...@googlegroups.com
This is a long shot, but MD5.new returns the same value as MD5.md5 for me..

Bryan Powell

unread,
May 9, 2009, 4:43:29 PM5/9/09
to hs...@googlegroups.com
Meh, I'm stupid today too; MD5.md5 just calls MD5.new...

philip reed

unread,
May 9, 2009, 9:18:10 PM5/9/09
to hs...@googlegroups.com
I'm officially the worlds farthest thing from the Rubyist, but FWIW I
was getting a failure that came from nowhere on an oldish Windows XP
laptop. I don't have it at hand but I'll check the version when I
get back to where I can.

So we're not alone. Sorta.


- Philip
--
Regards,

Philip Reed

philip reed

unread,
May 9, 2009, 9:21:05 PM5/9/09
to hs...@googlegroups.com
In editing that last message before sending it, I forgot to mention
that I was getting a failure *in the md5 library*. Actually it
looked like it could no longer find md5 at all.

If you were also on Win I'd guess that some M$ automatic update had
broken some dependency of md5. For this to break on two different
OSes could well be mere coincidence, though.

- Philip

Jeremy McAnally

unread,
May 9, 2009, 9:22:10 PM5/9/09
to hs...@googlegroups.com
No clue why that's broken. It works flawlessly for me...

On the other hand, you could do this:

irb(main):003:0> x = MD5.new
=> #<MD5: d41d8cd98f00b204e9800998ecf8427e>
irb(main):007:0> x << "blah blah blah"
=> #<MD5: 55e562bfee2bde4f9e71b8885eb5e303>

It's the same as doing this on a non-weirdly-broken version:

irb(main):009:0> y = MD5.new("blah blah blah")
=> #<MD5: 55e562bfee2bde4f9e71b8885eb5e303>

I can't figure out why that would be broken; I'll check on one of my
slices to see if I have an equivalent or later patchlevel. They may
have messed with it in preparation for 1.8.7 or something...

--Jeremy
--
http://jeremymcanally.com/
http://entp.com/
http://omgbloglol.com

My books:
http://manning.com/mcanally/
http://humblelittlerubybook.com/ (FREE!)

Ryan Waldron

unread,
May 10, 2009, 5:15:09 PM5/10/09
to hs...@googlegroups.com
On Sat, May 9, 2009 at 8:22 PM, Jeremy McAnally <jeremym...@gmail.com> wrote:

No clue why that's broken.  It works flawlessly for me...

I have it working on another server (actually made a call to my little adhearsion script handler, which played my script and made me smile).  So at least I know the code works.
 
On the other hand, you could do this:

   irb(main):003:0> x = MD5.new
   => #<MD5: d41d8cd98f00b204e9800998ecf8427e>
   irb(main):007:0> x << "blah blah blah"
   => #<MD5: 55e562bfee2bde4f9e71b8885eb5e303>

That's helpful, too.  I can actually do this (the << "blah" thing) on my busted version.  And what it returns in a string context is the same value returned by hexdigest, so I guess I can get around it.  But I still wish I knew.


Ryan Waldron

unread,
May 10, 2009, 5:23:26 PM5/10/09
to hs...@googlegroups.com
Yeah, OK, I think I found it.

In /usr/local/lib/ruby/site_ruby/1.8, I found this little file:

8 -rw-r--r--@ 1 root  wheel  257 Feb 20  2007 md5.rb

Let's take a peek inside, shall we?

---snip--snip---
$ cat md5.rb
# just for compatibility; requiring "md5" is obsoleted
#
# $RoughId: md5.rb,v 1.4 2001/07/13 15:38:27 knu Exp $
# $Id: md5.rb 1609 2001-07-13 20:06:14Z knu $

require 'digest/md5'

MD5 = Digest::MD5

class MD5
  def self.md5(*args)
    new(*args)
  end
end
---snip--snip---

Well, look at that.  Here's what the real md5.rb does here:

8 -rw-r--r--@ 1 root  wheel  431 Nov 24  2007 rubygems/digest/md5.rb

---snip--snip---
#!/usr/bin/env ruby
#--
# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
# All rights reserved.
# See LICENSE.txt for permissions.
#++

require 'digest/md5'

# :stopdoc:
module Gem
  if RUBY_VERSION >= '1.8.6'
    MD5 = Digest::MD5
  else
    require 'rubygems/digest/digest_adapter'
    MD5 = DigestAdapter.new(Digest::MD5)
    def MD5.md5(string)
      self.hexdigest(string)
    end
  end
end
# :startdoc:
---snip--snip---

That MD5.md5 expects a string and calls MD5.hexdigest (instead of MD5.new).  Looking still further, here's what 'self.new' ends up looking like (from rubygems/digest/digest_adapter):

def new
  seld
end

No arguments, right?  So when the old-and-weird md5.rb was calling new with *args, kaboom.

I have NO idea how that bogus md5.rb got there, or where it came from.  But it's gone now and Adhearsion (and me) are happy.
Reply all
Reply to author
Forward
0 new messages