Skip to content

Instantly share code, notes, and snippets.

@aquila12
Last active January 7, 2024 17:36
Show Gist options
  • Save aquila12/2210309ea7b4b9785ff06a5572342aa0 to your computer and use it in GitHub Desktop.
Save aquila12/2210309ea7b4b9785ff06a5572342aa0 to your computer and use it in GitHub Desktop.
A really simple Ruby script to check password pwnage (because https://github.com/philnash/pwned isn't simple enough)
#!/usr/bin/env ruby
# Checks whether passwords have been pwned
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'excon'
end
require 'csv'
require 'digest'
PP = Excon.new('https://api.pwnedpasswords.com', persistent: true)
def check_password(password)
password.chomp!
hash = Digest::SHA1.hexdigest(password)
prefix = hash.slice!(0..4)
key = "#{hash.upcase}:"
rows = PP.get(path: "/range/#{prefix}").body.lines
result = rows.bsearch { |r| r >= key }
return unless result&.start_with? key
count = result.split(':').last.to_i
puts "#{password}:#{count}"
end
# Read a firefox exported logins.csv
if ARGV.first == '--ff'
ARGV.shift
CSV.foreach(ARGV.shift, headers: true) { |r| check_password r['password'] }
# Iterate any files specified on the command line; or stdin
else
ARGF.each_line { |p| check_password p }
end
@aquila12
Copy link
Author

aquila12 commented Jan 7, 2024

Updated to add:

  • Inline bundler for a nicer http client
  • Swap to excon for persistent connection
  • Binary search for faster checking of retrieved pages
  • Ability to read from exported firefox CSV file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment