Created
March 11, 2019 20:10
-
-
Save olore/c772dfb9370afde9ee5be87d4fa4bcdb to your computer and use it in GitHub Desktop.
Have I Been Pwned, ruby style
This file contains hidden or 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
#!/usr/bin/env ruby | |
require 'net/http' | |
require 'digest' | |
url = 'https://api.pwnedpasswords.com/range' | |
input = ARGV[0] ? ARGV : ARGF.read.split | |
input.each do |pw| | |
password = pw.chomp | |
sha1 = Digest::SHA1.hexdigest(password).upcase | |
sha1_prefix = sha1[0..4] | |
sha1_suffix = sha1[5..-1] | |
uri = URI("#{url}/#{sha1_prefix}") | |
Net::HTTP.start(uri.host, uri.port, | |
:use_ssl => uri.scheme == 'https') do |http| | |
request = Net::HTTP::Get.new uri | |
hashes = http.request(request).body | |
found = hashes.split.find { |suffix| suffix.start_with?(sha1_suffix) } | |
if found | |
puts "UH OH! Found #{password} #{found.split(':')[1]} times" | |
else | |
puts "#{password} is safe" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment