1#!/usr/bin/perl -w
2
3# OpenLDAP {APR1} to Apache $apr1$ hash converter
4# (C) 2011 Devin J. Pohly
5# You may use this code freely.  It would be nice to be credited.
6
7use MIME::Base64;
8
9while (<>) {
10	($user, $hash) = split(/:/, $_);
11	unless ($hash =~ /^{APR1}/) {
12		print STDERR "Not an Apache MD5 hash\n";
13		next;
14	}
15
16	chomp $hash;
17	$hash = decode_base64(substr($hash, 6));
18	($hash, $salt) = (substr($hash, 0, 16), substr($hash, 16));
19	$hash = $hash;
20	$hash =~ s/(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)/$1$7$13$2$8$14$3$9$15$4$10$16$5$11$6\0\0$12/s;
21	$hash = encode_base64($hash);
22	chomp $hash;
23	$hash =~ s/(.)(.)(.)(.)/$4$3$2$1/gs;
24	unless ($hash =~ /AA$/) {
25		#print "Problem with hash\n";
26		next;
27	}
28	$hash =~ s/AA$//;
29	$hash =~ tr|A-Za-z0-9+/|./0-9A-Za-z|;
30	print "$user:\$apr1\$$salt\$$hash\n"
31}