#!/usr/bin/perl

=head1 NAME

htdigest-convert - converts a htdigest from decrypted format to hashed format

=head1 SYNOPSIS

  htdigest-convert < htdigest.decrypted > htdigest.hashed

=head1 DESCRIPTION

htdigest format is used by Apache to store passwords.

=head1 EXAMPLE

echo 'user1:realm1:Pa$$w0rd' > htdigest.decrypted
echo 'user2:realm1:$3:cr37' >> htdigest.decrypted
echo 'user2:realm2:::' >> htdigest.decrypted  # Password = ::
htdigest-convert < htdigest.decrypted > htdigest.hashed

=head1 AUTHOR

Ole Tange, http://ole.tange.dk

=head1 LICENSE

GPL http://www.gnu.org/copyleft/gpl.html

=head1 SEE ALSO

htdigest(1).

=cut

use Digest::MD5 qw(md5 md5_hex md5_base64);

while(<>) {
    /^(\S+?):(\S+?):(\S+)$/ or die ("Line is not 'user:realm:password': $_");
    $user=$1;
    $realm=$2;
    $password=$3;
    print "$user:$realm:",md5_hex("$user:$realm:$password"),"\n";
}
