This script checks a password is registered or not in Pwned Password. To keep secret on http request, the password will be encrypted with SHA1 hash.
Last active
January 22, 2019 07:20
-
-
Save kou1okada/573d792467d7b8cbed36cefd46d96d4a to your computer and use it in GitHub Desktop.
haveibeenpwned.sh - Check pwnage of password.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# | |
# haveibeenpwned.sh - Check pwnage of password. | |
# Copyright (c) 2019 Koichi OKADA. All rights reserved. | |
# This script is distributed under the MIT license. | |
# | |
SGR_reset="\e[0m" | |
SGR_fg_red_bold="\e[31;1m" | |
SGR_fg_green_bold="\e[32;1m" | |
function getpw () # [<PROMPT>] | |
# Get password with masked echo back. | |
{ | |
local prompt="${1:-Password: }" | |
local password="" | |
local ch | |
while IFS="" read -p "$prompt" -rsn1 ch; do | |
case "$ch" in | |
"") | |
break | |
;; | |
$'\x15') | |
prompt="${password//?/$'\b \b'}" | |
password="" | |
;; | |
$'\x08'|$'\x7f') | |
[ -n "$password" ] && prompt=$'\b \b' || prompt="" | |
password="${password%?}" | |
;; | |
*) | |
prompt="*" | |
password+="$ch" | |
;; | |
esac | |
done | |
[ -t 0 ] && echo >/dev/tty | |
echo -n "$password" | |
} | |
function haveibeenpwned () # [<password>] | |
# Check pwnage of password. | |
{ | |
local SHA1="$( (( 0 < $# )) && echo -n "$1" || getpw | sha1sum | awk '$0=toupper($1)' )" | |
wget -qO- https://api.pwnedpasswords.com/range/${SHA1:0:5} | grep ${SHA1:5} \ | |
&& echo -e "${SGR_fg_red_bold}Oh no - pwned!${SGR_reset}" \ | |
|| echo -e "${SGR_fg_green_bold}Good news - no pwnage found${SGR_reset}" | |
} | |
haveibeenpwned "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment