Skip to content

Instantly share code, notes, and snippets.

@Freaky
Created August 3, 2017 22:43
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 Freaky/4cb7ce8c107c3da2e4a8210356e8da25 to your computer and use it in GitHub Desktop.
Save Freaky/4cb7ce8c107c3da2e4a8210356e8da25 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'digest/sha1'
# search a file of "<sha1><crlf>"
def sha1_binsearch(io, target, linesize = 42, hashsize=40)
low, high = 0, (io.size / linesize) - 1
while low <= high
mid = (low + high) / 2
io.pos = mid * linesize
midval = io.read(hashsize)
if midval < target
low = mid + 1
elsif midval > target
high = mid - 1
else
return true
end
end
return false
end
db = File.new 'pwned-passwords-1.0.txt'
loop do
print "Password: "
pass = $stdin.gets.chomp
exit if pass.empty?
a = Time.new.to_f
hash = Digest::SHA1.hexdigest(pass).upcase
puts "Found: %s in %.2f ms" % [sha1_binsearch(db, hash), (Time.new.to_f - a) * 1000]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment