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
(PASSWORD="SEcRe7PwD";SALT="$(openssl rand -base64 3)";SHA1=$(printf "$PASSWORD$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 $!;
}
GarryField Reviewed by GarryField on . How to generate an .htpasswd file without having Apache tools installed ? i would like to protect some of my folders on server but i wonder how can i generate .htpasswd file without apache tools installed on my linux server ? Rating: 5