Forum: Ruby How to get an MD5 Base64 hash?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Sean H. (Guest)
on 2005-12-13 18:56
(Received via mailing list)
Hi everyone,

I'm trying to MD5 passwords for insertion into our LDAP.  I can't seem
to get the right combination of Digest, BD5, and Base64 calls to get a
string that works.

Most passwords end up looking like this:

{MD5}qLdnu5zwk43H9AYD8zmH5Q==

I can manually tack on the {MD5}, of course.  I've tried things along
these lines:

require 'digest/md5'
np = Digest::MD5.new
np << "hello"
require 'base64'

Base64.b64encode(np.to_s)  # =>
NWQ0MTQwMmFiYzRiMmE3NmI5NzE5ZDkxMTAxN2M1OTI=

Base64.b64encode(np.hash.to_s) # => OTAzMjEy

# From http://rubyforge.org/snippet/detail.php?type=snipp...
h1=[].clear
16.times{ h1.push(np.hexdigest.to_s.slice!(0,2).hex) }
[h1.pack("C*")].pack("m")  # => XV1dXV1dXV1dXV1dXV1dXQ==

None of the resulting strings works.  What am I missing?

Thank you!

Sean
Bob S. (Guest)
on 2005-12-13 19:08
(Received via mailing list)
Sean H. wrote:
> I can manually tack on the {MD5}, of course.  I've tried things along
> these lines:
>
> require 'digest/md5'
> np = Digest::MD5.new
> np << "hello"
> require 'base64'
>
> Base64.b64encode(np.to_s)  # => NWQ0MTQwMmFiYzRiMmE3NmI5NzE5ZDkxMTAxN2M1OTI=

try:

   Base64.b64encode(np.digest)
   => "XUFAKrxLKna5cZ2REBfFkg==\n"

to_s gives you the digest as a string of hex digits.

Documentation definitely lacking in that module ;)
Sean H. (Guest)
on 2005-12-13 19:45
(Received via mailing list)
That's it!  Thank you so much!

Agreed on the docs.  I was hoping to even find someone else
documenting it elsewhere (blog, FAQ, etc), but no dice.

Thanks again!

Sean
This topic is locked and can not be replied to.