Skip to content

Instantly share code, notes, and snippets.

@marshallmick007
Forked from schmich/pwned-interactive.rb
Created February 21, 2018 22:48
Show Gist options
  • Save marshallmick007/98034e0c7aa82fd98f345df11caf0492 to your computer and use it in GitHub Desktop.
Save marshallmick007/98034e0c7aa82fd98f345df11caf0492 to your computer and use it in GitHub Desktop.
Check if a password has been pwned with the Pwned Passwords V2 API
require 'io/console'
require 'open-uri'
require 'digest'
puts "The 5-character prefix of the password's SHA-1 hash will be sent."
puts "For details, see https://www.troyhunt.com/ive-just-launched-pwned-passwords-version-2/"
print 'Password (hidden): '
password = $stdin.noecho(&:gets).strip
puts
hash = Digest::SHA1.hexdigest(password).upcase
prefix = hash[0...5]
url = "https://api.pwnedpasswords.com/range/#{prefix}"
print "Requesting #{url}. Continue (y/n)? "
if $stdin.gets.strip.downcase[0] != 'y'
puts 'Canceled.'
exit
end
pwned = open(url) do |response|
Hash[response.each_line.map { |line|
suffix, count = line.strip.split(':')
[(prefix + suffix).upcase, count.to_i]
}]
end
count = pwned[hash]
print "Password with SHA-1 hash #{hash} "
if count
puts "has been pwned. Seen #{count} time#{count == 1 ? '' : 's'}."
else
puts "has not been pwned."
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment