Skip to content

Instantly share code, notes, and snippets.

@mloughran
Created December 4, 2020 17:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mloughran/2833e61730344d53f4acc06153bd3e16 to your computer and use it in GitHub Desktop.
Save mloughran/2833e61730344d53f4acc06153bd3e16 to your computer and use it in GitHub Desktop.
Interactive tool to check passwords against the HIBP API
require 'digest/sha1'
require 'net/http'
API_ROOT = "https://api.pwnedpasswords.com/range/"
def split_sha(password)
sha = Digest::SHA1.hexdigest(password).upcase
[sha[0...5], sha[5..]]
end
def check_api(prefix)
uri = URI("#{API_ROOT}/#{prefix}")
resp = Net::HTTP.get_response(uri)
if resp.code.to_i != 200
puts "API failed, code: #{resp.code}"
exit 1
end
return resp.body.lines.map { |line| line.chomp.split(":") }.to_h
end
def check(password)
prefix, remainder = split_sha(password)
check_api(prefix)[remainder]
end
# main
loop do
print "Enter a password to check: "
password = gets.chomp
if count = check(password)
puts "Password found. Count: #{count}"
else
puts "Password not found"
end
puts
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment