Skip to content

Instantly share code, notes, and snippets.

@coventry
Created August 3, 2017 22:53
Show Gist options
  • Save coventry/5df7885fb0d5caeabb39fcd0e2bf4864 to your computer and use it in GitHub Desktop.
Save coventry/5df7885fb0d5caeabb39fcd0e2bf4864 to your computer and use it in GitHub Desktop.
Simple binary search of Troy Hunt's password list
#!/usr/bin/python
import sys
import sha
f = open('/tmp/pwned-passwords-1.0.txt')
minpos = 0
f.seek(0, 2)
maxpos = f.tell() / 42 # Number of hashes
pw = sys.argv[1]
pwhash = sha.new(pw).hexdigest().upper()
def read_hash(position):
f.seek(position * 42)
return f.readline().strip()
while True:
if (maxpos - minpos) <= 1:
if pwhash in (read_hash(minpos), read_hash(maxpos)):
print 'Match'
else:
print 'No match'
break
position = (minpos + maxpos) / 2
chash = read_hash(position)
if chash == pwhash:
print 'Match!'
break
elif pwhash < chash:
maxpos = position
elif pwhash > chash:
minpos = position
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment