I need to calculate hash which starts with some string. My input is:
- other hash (fixed)
- some variadic data occupying 5 bytes which I need to prepare in a way so that the result hash starts with e.g. 'ABC'
I am somewhat new in Python, and I came up with this ugly solution (in function calculate). To speed up processing I use executors to run this on all cores.
Can someone help me figure out to make this function 'calculate' less ugly? How to do it better? How to iterate through all permutations of 5 bytes of my variadic data? And finally, what do I do wrong or non-pythonic here?
import hashlib
import concurrent.futures
import copy
input_hash = [0x79,0xaf,0x37,0xc4,0x32,0x8e,0x7b,0x67,0xb1,0xfa,0x76,0x36,0x11,0x21,0xa4,0xdd,0x6c,0x29,0xf0,0x6b,0x50,0x67,0x57,0x16,0x0b,0xee,0x30,0x32,0x2a,0x05,0x9e,0x75]
def calculate(pars):
print(pars)
for x in range(pars[0],pars[1]):
whole = []
whole.extend(bytearray(input_hash))
whole.extend(bytes([x]))
copy1 = copy.deepcopy(whole)
for y in range(256):
whole = copy.deepcopy(copy1)
whole.extend(bytes([y]))
copy2 = copy.deepcopy(whole)
for z in range(256):
whole = copy.deepcopy(copy2)
whole.extend(bytes([z]))
copy3 = copy.deepcopy(whole)
for a in range(256):
whole = copy.deepcopy(copy3)
whole.extend(bytes([a]))
copy4 = copy.deepcopy(whole)
for b in range(256):
whole = copy.deepcopy(copy4)
whole.extend(bytes([b]))
whole.extend(bytes([0]*2))
m = hashlib.sha256()
m.update(bytearray(whole))
d = m.hexdigest()
if d.startswith('ABC'):
print('success!, x = %, y = %, z = %, a = %, b = %' % x, y, z, a, b)
return
data = [(0,33),(33,67),(67,101),(101,135),(135,169),(169,203),(203,237),(237,256)]
with concurrent.futures.ProcessPoolExecutor() as executor:
res = executor.map(calculate, data)
for r in res:
pass