【问题标题】:How to make this sha256 pow如何制作这个 sha256 pow
【发布时间】:2021-06-11 19:55:15
【问题描述】:

这是一个简单的工作证明脚本,它采用 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

【问题讨论】:

  • 这不是问题
  • 我需要制作一个简单的 CLI 脚本,当给定一个 64 字节的字符串时,它会找到一个合适的 4 字节前缀,以便前缀的 SHA256 哈希与原始字节字符串相结合, 最后两个字节为 0xca, 0xfe。脚本应该期望原始字符串以十六进制格式传递,并且应该返回两行,第一行是找到的 SHA256 字符串,第二个是使用的 4 字节前缀(十六进制格式)。
  • @leftjoin 平均而言,如果你散列 2^31 个均匀随机值,那么你应该会找到这样的散列值。

标签: python algorithm cryptography md5 sha256


【解决方案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])

这样输出哈希值和前 4 个字节:

c19df416e581278d028f20527b96c018f313e983f0a9bda4914679bb482d14f6 c19d
b5662892a57d57b6d904fa6243fae838c28aaebc190fc885ee2e20de6fbcaddb b566
a9c0a662c81abe40bca7d250e93965285e0aea74f2f3edde704015c11e98cdeb a9c0
635f19669b8b48cd78f05bfe52ccf4bfbdb55e544486d71404f5fa786d93e5be 635f

永无止境。添加您的逻辑以退出循环。

【讨论】:

  • 感谢您的帮助,但收到此错误 hmmTraceback(最近一次调用最后一次):文件“main.py”,第 10 行,在 curr_hash = sha256(line.encode()).hexdigest () NameError: name 'line' is not defined
  • 已修复抱歉我刚刚添加了字符串
  • @ToniDev 用 curr_string 替换行。固定
猜你喜欢
  • 2022-11-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-18
  • 1970-01-01
  • 1970-01-01
  • 2022-01-17
  • 2014-02-22
  • 1970-01-01
相关资源
最近更新 更多