Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

loop through all possible combination of 8 bytes

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading