首页 > 解决方案 > 如何制作这个 sha256 pow

问题描述

这是一个简单的工作证明脚本,它在 md5 算法中。有什么办法可以用它把它转移到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

标签: pythonalgorithmcryptographymd5sha256

解决方案


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])

这会输出散列和前 4 个字节,如下所示:

c19df416e581278d028f20527b96c018f313e983f0a9bda4914679bb482d14f6 c19d
b5662892a57d57b6d904fa6243fae838c28aaebc190fc885ee2e20de6fbcaddb b566
a9c0a662c81abe40bca7d250e93965285e0aea74f2f3edde704015c11e98cdeb a9c0
635f19669b8b48cd78f05bfe52ccf4bfbdb55e544486d71404f5fa786d93e5be 635f

并且永远不会结束。添加您的逻辑以退出循环。


推荐阅读