Created
December 4, 2020 17:37
-
-
Save mloughran/2833e61730344d53f4acc06153bd3e16 to your computer and use it in GitHub Desktop.
Interactive tool to check passwords against the HIBP API
This file contains 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
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