How do I generate an .htpasswd file without having Apache tools installed?

http://wiki.nginx.org/Faq#How_do_I_generate_an_htpasswd_file_without_having_Apache_tools_installed.3F

In Linux (and other Posix): given users John, Mary, Jane and Jim and passwords V3Ry, SEcRe7, V3RySEcRe7 and SEcRe7PwD, in order to generate a password file named .htpasswd, you would issue:
printf “John:$(openssl passwd -crypt V3Ry)\n” >> .htpasswd # this example uses crypt encryption
printf “Mary:$(openssl passwd -apr1 SEcRe7)\n” >> .htpasswd # this example uses apr1 (Apache MD5) encryption
printf “Jane:$(openssl passwd -1 V3RySEcRe7)\n” >> .htpasswd # this example uses MD5 encryption
(PWD=”SEcRe7PwD”;SALT=”$(openssl rand -base64 3)”;SHA1=$(printf “$PWD$SALT” | openssl dgst -binary -sha1 | \
sed ‘s#$#'”$SALT”‘#’ | base64);printf “Jim:{SSHA}$SHA1\n” >> .htpasswd) # this example uses SSHA encryption
Or, you may use the following crypt.pl Perl script. Simply save it as e.g. crypt.pl and chmod 700 crypt.pl in order to be able to execute it.
#!/usr/bin/perl
use strict;

chomp(my $filename=$ARGV[0]);
chomp(my $username=$ARGV[1]);
chomp(my $password=$ARGV[2]);

if (!$filename || !$username || !$password) {
print “USAGE: ./crypt.pl filename username password\n\n”;
} else {
open my $fh, “>>”, $filename or die $!;
print $fh $username . “:” . crypt($password, $username) . “\n”;
close $fh or die $!;
}
Or, you may use the htpasswd.py python script.

One thought on “How do I generate an .htpasswd file without having Apache tools installed?

Leave a Reply

Your email address will not be published. Required fields are marked *

eighteen − 6 =