Skip to content

Instantly share code, notes, and snippets.

@chulkilee
Last active September 24, 2020 06:36
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save chulkilee/6798626 to your computer and use it in GitHub Desktop.
Save chulkilee/6798626 to your computer and use it in GitHub Desktop.
#!/bin/bash
# https://httpd.apache.org/docs/2.2/misc/password_encryptions.html
HTPASSWD=$1
USERNAME=$2
PASSWORD=$3
ENTRY=`cat $HTPASSWD | grep "^$USERNAME:"`
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
@evan-burke
Copy link

Thanks for writing this! For more security-conscious users, I might suggest prompting the user for the password rather than specifying it on the command line, in order to keep it out of bash_history. Like this:

echo "enter the password for user $USERNAME"
read PASSWORD

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment