Skip to content

Instantly share code, notes, and snippets.

@boppy
Forked from ckujau/htpasswd.sh
Last active July 24, 2016 12:58
Show Gist options
  • Save boppy/2fa358c125075a4e07011d277e643456 to your computer and use it in GitHub Desktop.
Save boppy/2fa358c125075a4e07011d277e643456 to your computer and use it in GitHub Desktop.
Generate an .htpasswd file without the Apache tools
#!/bin/bash
#
# Generate an SSHA password for .htpasswd files
#
# Output can be redirected (while asking for password on screen).
# Password has to be entered twice.
#
# * https://www.nginx.com/resources/wiki/community/faq/#how-do-i-generate-an-htpasswd-file-without-having-apache-tools-installed
# * https://nginx.org/en/docs/http/ngx_http_auth_basic_module.html#auth_basic_user_file
#
if [ -z "$1" ]; then
echo "Usage: $(basename $0) [user]"
exit 1
fi
NAME="$1"
printf "Password: " > /dev/tty
read -s PASSWORD
echo > /dev/tty
printf "Password again: " > /dev/tty
read -s PASSWORD2
echo > /dev/tty
if [ $PASSWORD = $PASSWORD2 ]; then
SALT=$(openssl rand -base64 3)
SHA1=$(printf "${PASSWORD}${SALT}" | openssl dgst -binary -sha1 | xxd -ps | sed 's#$#'"`echo -n $SALT | xxd -ps`"'#' | xxd -r -ps | base64)
echo "$NAME:{SSHA}$SHA1"
else
echo "Passwords don't match" > /dev/tty
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment