I want to do something like this:
for i in range('\xff'*8):
hash = b'challenge' + i
inp = hashlib.sha256(hash).digest()
I don’t know how i can get all the possible combinations of 8 bytes with bitwise operations.
>Solution :
You can do this, but using struct would be preferred, I’ve also added the zero check (increase 2 to 26…):
start = b'\0' * 2
import hashlib
for i in range(256 ** 8):
byte = b'challenge' + bytes.fromhex("%016x" % i)
inp = hashlib.sha256(byte).digest()
if inp.startswith(start):
print(byte, inp)
break
Output:
b'challengeH\x11\x00\x00\x00\x00\x00\x00' b'\x00\x00\xb6z\\\x97.\xca\t\xc2\x1e\xd6\x07\x17\xd1\xba\xf1O\xf6\x8f x("\x871\x92\xa9\xf35\x95\xb6'
...
I mean you’re already using hashlib, so you could as well use another standard library:
start = b'\0' * 2
import hashlib, struct
for i in range(256 ** 8):
byte = b'challenge' + struct.pack("<Q", i)
inp = hashlib.sha256(byte).digest()
if inp.startswith(start):
print(byte, inp)
break