Skip to content

Instantly share code, notes, and snippets.

@sturman
Last active May 13, 2024 06:29
Show Gist options
  • Save sturman/35e9637af4ccb57b5b0113a51f5b7f01 to your computer and use it in GitHub Desktop.
Save sturman/35e9637af4ccb57b5b0113a51f5b7f01 to your computer and use it in GitHub Desktop.
Bash script for checking if hash matches the password. May be useful during configuring Apache or nginx basic authentication
#!/usr/bin/env bash
# https://httpd.apache.org/docs/2.4/misc/password_encryptions.html
# $ htpasswd -nbm sturman Passw0rd > .htpasswd
# $ ./check_pass.sh .htpasswd sturman Passw0rd
HTPASSWD=$1
USERNAME=$2
PASSWORD=$3
ENTRY=$(grep "^$USERNAME:" < "$HTPASSWD")
HASH=$(echo "$ENTRY" | cut -f 2 -d :)
SALT=$(echo "$HASH" | cut -f 3 -d \$)
RESULT=$(openssl passwd -apr1 -salt "$SALT" "$PASSWORD")
echo "File: $HTPASSWD"
echo "Username: $USERNAME"
echo "Entry: $ENTRY"
echo "Hash: $HASH"
echo "Salt: $SALT"
echo "password to check: $PASSWORD"
echo "openssl result: $RESULT"
if [ "$RESULT" = "$HASH" ]
then
echo "OKAY"
else
echo "NOT MATCHED"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment