MD5 Passwords for LDAP in Ruby

Tomasz Marciniak

2013-12-03

As a sysadmin I love Ruby for having so many useful tools in it’s stdlib. For example, when you need to change userPassword value in LDAP, you can use:

irb(main)> require 'base64'
=> true
irb(main)> require 'digest/md5'
=> true
irb(main)> puts Digest::MD5.base64digest 'foobar'
OFj2IjCsPJFfMAxmQxLGPw==
=> nil
irb(main)> Base64.encode64 '{MD5}OFj2IjCsPJFfMAxmQxLGPw=='
=> "e01ENX1PRmoySWpDc1BKRmZNQXhtUXhMR1B3PT0=\n"

or the same in one shot directly from shell:

ruby -rbase64 -r'digest/md5' -e 'puts Base64.encode64( "{MD5}" + Digest::MD5.base64digest( "foobar" ) )'
e01ENX1PRmoySWpDc1BKRmZNQXhtUXhMR1B3PT0=

This is useful for processing LDIF files. See Ruby documentation for other digest methods if you don’t like MD5.