Skip to content

Instantly share code, notes, and snippets.

@olore
Created March 11, 2019 20:10
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 olore/c772dfb9370afde9ee5be87d4fa4bcdb to your computer and use it in GitHub Desktop.
Save olore/c772dfb9370afde9ee5be87d4fa4bcdb to your computer and use it in GitHub Desktop.
Have I Been Pwned, ruby style
#!/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