Last active
January 17, 2019 20:56
-
-
Save oprypin/a7bef68fc4804ba384178ee129465e74 to your computer and use it in GitHub Desktop.
Check your passwords against https://haveibeenpwned.com/Passwords
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
# Check your passwords against https://haveibeenpwned.com/Passwords | |
# Usage: | |
# crystal check_passwords.cr -- pwned-passwords-sha1-ordered-by-hash*.txt | |
# Enter your passwords one per line, then press Return twice. Or pipe them in. | |
require "digest/sha1" | |
passwords = Array(String).new | |
until (line = gets(chomp: true) || "").empty? | |
passwords << line | |
end | |
ARGV.each do |filename| | |
File.open(filename) do |file| | |
passwords.each do |password| | |
hash = Digest::SHA1.hexdigest(password).upcase | |
(0u64...File.size(filename)).bsearch { |pos| | |
file.seek(pos) | |
# Find the beginning of the line. | |
pos -= 40 - file.read_line(':', chomp: true).size | |
file.seek(pos) | |
line = file.read_line(':', chomp: true) | |
raise "Malformed file" if line.size != 40 | |
if line == hash | |
count = file.read_line(chomp: true) | |
puts "#{password} \u00D7#{count}" | |
break | |
end | |
line >= hash | |
} | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
nice