#!/usr/bin/env bash # Usage: ./scripts/create_user.sh # Prints a line suitable for appending to users.env set -euo pipefail if [ $# -ne 2 ]; then echo "Usage: $0 " >&2 exit 1 fi USERNAME="$1" PASSWORD="$2" # bcrypt the peppered input "username:password" PEPPERED="${USERNAME}:${PASSWORD}" # Try Python 3 with bcrypt first, then fall back to htpasswd if python3 -c "import bcrypt" 2>/dev/null; then HASH=$(python3 -c " import bcrypt, sys pw = sys.argv[1].encode() print(bcrypt.hashpw(pw, bcrypt.gensalt(rounds=12)).decode()) " "$PEPPERED") elif command -v htpasswd &>/dev/null; then HASH=$(htpasswd -bnBC 12 "" "$PEPPERED" | tr -d ':\n' | sed 's/^\$/\$/') else echo "Error: install 'bcrypt' (pip install bcrypt) or 'htpasswd' (apache2-utils)" >&2 exit 1 fi echo "${USERNAME}:${HASH}"