Skip to content

Instantly share code, notes, and snippets.

@zeusdeux
Last active June 7, 2023 08:08
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zeusdeux/84b29b2ab277047dfe4591d0e484e71e to your computer and use it in GitHub Desktop.
Save zeusdeux/84b29b2ab277047dfe4591d0e484e71e to your computer and use it in GitHub Desktop.
Check if any passwords have been compromised using HIBP's password API (https://haveibeenpwned.com/API/v2#PwnedPasswords). Your password never leaves your local system!
#!/usr/bin/env bash
# enable when debugging
# set -o errexit
# set -o errtrace
# set -o xtrace
# set -o nounset
# set -o pipefail
plsno () {
if [ -z $1 ]; then
echo -e "\e[31mPlease provide a password to check if it has been pwned." 1>&2 # write to stderr
echo -e "\e[34mUsage: <space so that it doesn't get saved to bash_history>plsno <password>\e[0m" 1>&2
return 1
fi
local PASS_SHA1=$(echo -n $1 | shasum | cut -d' ' -f1)
local PASS_SHA1_PREFIX=$(echo -n $PASS_SHA1 | cut -c1-5)
local PASS_SHA1_SUFFIX=$(echo -n $PASS_SHA1 | cut -c6-)
local PASS_PWNED=$(curl -s "https://api.pwnedpasswords.com/range/${PASS_SHA1_PREFIX}" | grep -i "${PASS_SHA1_SUFFIX}")
local PASS_PWNED_COUNT=$(echo -n $PASS_PWNED | cut -d':' -f2 | tr -s ' ')
if [ -z "${PASS_PWNED}" ]; then
echo -e "\e[32mThis password is \e[1mNOT PWND\e[21m. You can sleep peacefully at night.\e[0m"
else
echo -ne "\e[1m\e[31m\"${1}\" is PWNED! :( "
echo "Please change this password NOW on all affected accounts."
echo -ne "\e[0m\e[90mTimes found ->" $PASS_PWNED_COUNT
echo -e "\e[0m"
fi
return 0
}
@zeusdeux
Copy link
Author

Usage: plsno "password"

@zeusdeux
Copy link
Author

The password never leaves your computer. The script leverages k-Anonymity.

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