How to get an MD5 Base64 hash?

S

Sean Hussey

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=3D=3D

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

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

Base64.b64encode(np.to_s) # =3D> NWQ0MTQwMmFiYzRiMmE3NmI5NzE5ZDkxMTAxN2M1O=
TI=3D

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

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

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

Thank you!

Sean
 
B

Bob Showalter

Sean said:
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=

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 ;)
 
S

Sean Hussey

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
 

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

Staff online

Members online

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top