Ein paar Fehler der Fingerübung (überflüssige/falsche Zeilenumbrüche im Ergebnis) entfernt, strict hinzugefügt, mehr Tests. Verschönert.
#!/usr/bin/perl
use strict;
sub htpasswd_wrapper {
## return a fine hashed password for Apache 2.4 oder 2.2
## usage htpasswd_wrapper($passwd_clear, '2.4')
## or htpasswd_wrapper($passwd_clear, '2.2')
my $passwd_clear = shift;
my $version = shift;
my $sys = '';
my $passwd_hash = '';
$passwd_clear =~ s/\"/\\\"/g;
$passwd_clear =~ s/\`/\\\`/g;
if ( $version eq "2.2" ) {
$sys = "echo \"$passwd_clear\" | /usr/bin/htpasswd -in \"USER\";";
} elsif ( $version eq "2.4" ) {
$sys = "echo \"$passwd_clear\" | /usr/bin/htpasswd -Bin \"USER\";";
}
if ($sys) {
$passwd_hash = `$sys`;
$passwd_hash =~ s/\s+$//;
$passwd_hash =~ s/^USER://;
return $passwd_hash;
} else {
return '';
}
}
## Tests:
my $passwd_hash;
$passwd_hash = htpasswd_wrapper("hallo", '2.4');
print $passwd_hash . "\n";
$passwd_hash = htpasswd_wrapper("hallo", '2.4');
print $passwd_hash . "\n";
print '-' x 60 . "\n";
$passwd_hash = htpasswd_wrapper("ha'llo", '2.4');
print $passwd_hash . "\n";
$passwd_hash = htpasswd_wrapper('ha"llo', '2.4');
print $passwd_hash . "\n";
$passwd_hash = htpasswd_wrapper('ha`ll`o', '2.4');
print $passwd_hash . "\n";
$passwd_hash = htpasswd_wrapper('ha` | less`o', '2.4');
print $passwd_hash . "\n";