Skip to content

Instantly share code, notes, and snippets.

@opieters
Created April 25, 2016 08:04
Show Gist options
  • Save opieters/87cc5d261265247d9e0cb6761861fd9c to your computer and use it in GitHub Desktop.
Save opieters/87cc5d261265247d9e0cb6761861fd9c to your computer and use it in GitHub Desktop.
from Crypto.Hash import SHA256
import struct, random, yaml, time
# hash to match
h = SHA256.new(b"123456")
ref = int(h.hexdigest(),16)
print(ref)
# config
treshhold = 20 # require AT LEAST 20 matching initial bits!
matches, start_value, max_match, max_value = [], 0, 0, 0
# generate a random start value
for i in range(32):
start_value = (start_value << 8) + random.randint(0, 2^8-1)
print(start_value)
# run till keyboard interrupt and then save file
try:
value = start_value
while(True):
# update hash
h = SHA256.new(str.encode("%d" % value))
# extract infomation into int
new = int(h.hexdigest(), 16)
# compare bits
v, i = format(ref ^ new, '0256b'), 0
while i < 256 and int(v[i]) == 0:
i += 1
# write to file if enough similarity
if i>=treshhold:
matches.append([value, i])
print("Match (%03d)" % i)
if max_match < i:
print("NEW MAX MATCH FOUND! %d" % i)
max_match = i
max_value = value
# update value
value += 1
except KeyboardInterrupt:
print("Stopped at: %d" % value)
with open("hash-file-%f.yml" % time.time(), 'w') as f:
data = {"matches": matches, "start": start_value, "end": value}
f.write(yaml.dump(data))
print("Max (%d): %d" % (max_match, max_value))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment