Skip to content

Instantly share code, notes, and snippets.

@ianlevesque
Forked from marshallmick007/pwned.rb
Created February 22, 2018 03:37
Show Gist options
  • Save ianlevesque/a6fc26eac0f2c01d49eca40e8de77f59 to your computer and use it in GitHub Desktop.
Save ianlevesque/a6fc26eac0f2c01d49eca40e8de77f59 to your computer and use it in GitHub Desktop.
Check an entire list of passwords against the Pwned Passwords V2 API
#!/usr/bin/env ruby
require 'io/console'
require 'open-uri'
require 'digest'
IO.foreach("passwords.txt") do |line|
password = line.strip
hash = Digest::SHA1.hexdigest(password).upcase
prefix = hash[0...5]
url = "https://api.pwnedpasswords.com/range/#{prefix}"
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 #{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
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment