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.
CodePudding user response:
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("6x" % i)
inp = hashlib.sha256(byte).digest()
if inp.startswith(start):
print(byte, inp)
break
Output:
b'challenge\x00\x00\x00\x00\x00\x00*\xde' b'\x00\x00\x9a\x84H\xbc\x07\xd7\xdc\x07#\xb8\x08A\xb1&\xdcD\xb0 \x84\\9y#\xc9\xcf\xaa\xff\xb7\xbf\x9a'
...
I mean you're already using hashlib, so you could as well use another standard library:
start = b'\0' * 2
import hashlib, struct
if struct.pack("Q", 1) == struct.pack(">Q", 1):
print("Using faster method:")
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
else:
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