This howto article shows you how to add a new user and password combination to a .htaccess file, for use with Nginx or another web server. While these commands below work great with Nginx, they aren’t limited to just being used with Nginx. It will work with Apache too, however the htpasswd tool that comes with Apache will probably make life easier.
In these examples, replace “testuser” and “Pass123” with the real username and password you wish to add.
(The examples below are based on the examples from http://wiki.nginx.org/Faq)
Using crypt encryption:
printf "testuser:$(openssl passwd -crypt Pass123)\n" >> .htpasswd
Using apr1 (Apache MD5) encryption:
printf "testuser:$(openssl passwd -apr1 Pass123)\n" >> .htpasswd
Using MD5 encryption:
printf "testuser:$(openssl passwd -1 Pass123)\n" >> .htpasswd
Using SSHA (Salted SHA-1) encryption:
(USERNAME="testuser";PWD="Pass123";SALT="$(openssl rand -base64 3)";SHA1=$(printf "$PWD$SALT" | \ openssl dgst -binary -sha1 | sed 's#$#'"$SALT"'#' | base64); \ printf "$USERNAME:{SSHA}$SHA1\n" >> .htpasswd)