1

This is a script that is a simple proof of work and it's in md5 algo. Is there any way I can use it to transfer it to sha256?

# proof-of-work.py
import md5

string = "1"

complete = False
n = 0

while complete == False:
    curr_string = string + str(n)
    curr_hash = md5.new(curr_string).hexdigest()
    n = n + 1

    # slows performance drastically
    ## print curr_hash 

    if curr_hash.startswith('000000'):
        print curr_hash
        print curr_string
        complete = True
jps
  • 20,041
  • 15
  • 75
  • 79
Toni Dev
  • 43
  • 10
  • THats not issues – Toni Dev Mar 14 '21 at 09:38
  • I need to make a simple CLI script, that when given a 64-byte string, it finds a suitable 4-byte prefix so that, a SHA256 hash of the prefix combined with the original string of bytes, has two last bytes as 0xca, 0xfe. Script should expect the original string to be passed in hexadecimal format and should return two lines, first being the SHA256 string found and second 4-byte prefix used (in hexadecimal format). – Toni Dev Mar 14 '21 at 09:44
  • 1
    @leftjoin On average, if you hash 2^31 uniform random values then you are expected to find such hash value. – kelalaka Mar 14 '21 at 13:54

1 Answers1

1
from hashlib import sha256

string = "1"

complete = False
n = 0

while complete == False:
    curr_string = string + str(n)
    curr_hash = sha256(curr_string.encode()).hexdigest()
    n = n + 1
    
    print(curr_hash + ' ' + curr_hash[:4])

This outputs hashes and first 4 bytes like this:

c19df416e581278d028f20527b96c018f313e983f0a9bda4914679bb482d14f6 c19d
b5662892a57d57b6d904fa6243fae838c28aaebc190fc885ee2e20de6fbcaddb b566
a9c0a662c81abe40bca7d250e93965285e0aea74f2f3edde704015c11e98cdeb a9c0
635f19669b8b48cd78f05bfe52ccf4bfbdb55e544486d71404f5fa786d93e5be 635f

and never ends. Add your logic to exit the loop.

leftjoin
  • 36,950
  • 8
  • 57
  • 116