Nic
Nic

Reputation: 703

MySQL MD5 hash to base64

I have a mysql db that stores all my users info (username, pass, etc). I am building an LDAP server with back-sql and everything works fine except the password field. All passwords are basic MD5 hashes so

select MD5('testPass01')

returns

428a65ed9de7dbf8ef7d08f884528440

Apparently that is a hexpair representation of a binary value. LDAP will understand a base64 encoded string which is something like this

{MD5}Qopl7Z3n2/jvfQj4hFKEQA==

I found a perl script that does the conversion

#!/usr/bin/perl
use MIME::Base64;
use strict;
my @md5 = split "",$ARGV[0];
my @res;
for (my $i = 0 ; $i < 32 ; $i+=2)
{
my $c = (((hex $md5[$i]) << 4) % 255) | (hex $md5[$i+1]);
$res[$i/2] = chr $c;
print $c;
}
print "{MD5}".encode_base64(join "", @res);
#-------------------------------------------#

My question is: Is it possible to do the conversion using SQL through a stored procedure? I cannot install MySQL server 5.6 which includes base64 encode and decode functions

Thanks

Upvotes: 2

Views: 4390

Answers (1)

Nic
Nic

Reputation: 703

It works perfectly. Using the function in the comments like this will produce a base64 password that LDAP will accept.

select BASE64_ENCODE(unhex('mysql-md5-encoded-password')

Upvotes: 2

Related Questions