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