From e1f6c0af2fea59c1def545d623ec1ae94aa5f0e7 Mon Sep 17 00:00:00 2001 From: Elvis Date: Thu, 30 May 2019 19:02:04 -0400 Subject: [PATCH] Create pwned.mos (#26) macOS bash version --- pwned.mos | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100755 pwned.mos diff --git a/pwned.mos b/pwned.mos new file mode 100755 index 0000000..dc7eff8 --- /dev/null +++ b/pwned.mos @@ -0,0 +1,41 @@ +#!/bin/bash +# FAIR License, Copyright (c) 2019 72Zn +# Usage of the works is permitted provided that this instrument is retained +# with the works, so that any entity that uses the works is notified of this +# instrument. +# DISCLAIMER: THE WORKS ARE WITHOUT WARRANTY. + +# usage examples: +# ./pwned.mos [pw1] [pw2] ... +# ./pwned.mos < +# echo pw | ./pwned.mos + +PWNAPI="https://api.pwnedpasswords.com/range" + +lookup_pwned_api() { + local pass="$1" + local pwhash=$(printf "%s" "$pass" | shasum -a 1 | cut -d" " -f1) + local curlrv="$(curl -s "$PWNAPI"/"${pwhash:0:5}")" + [ -z "$curlrv" ] && echo "$pass could not be checked" && return + local result="$(echo "$curlrv" | grep -i "${pwhash:5:35}")" + + if [ -n "$result" ]; then + local occ="$(printf "%s" "${result}" | cut -d: -f2 | sed 's/[^0-9]*//g')" + printf "%s was found with %s occurances (hash: %s)\n" "$pass" "$occ" "$pwhash" + else + printf "%s was not found\n" "$pass" + fi +} + +if [ "$#" -lt 1 ]; then + # read from file or stdin + while read -r pw; do + lookup_pwned_api $pw + done +else + # read arguments + for pw in "$@"; do + lookup_pwned_api $pw + done +fi +