python - 如何制作这个 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
解决方案
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
并且永远不会结束。添加您的逻辑以退出循环。
推荐阅读
- javascript - Owlcarousel 2.3.4 版和自定义 owl carousel addClassActive 不起作用
- apache - Apache 重写以对 admin.example.com 使用不同的 index.php
- verilog - 在verilog中创建时间延迟的问题
- python - 使用 pandas groupby 获取与最小值对应的行
- android - 无法为 android 编译 x264 库,错误描述:clang:error no such file or directory
- git - RunCloud 部署:您对以下文件 (package-locked.json) 的本地更改将被合并覆盖
- node.js - 使用 Node.js 在 Firebase 中使用 Typescript 进行读写
- python - Python Windows 服务无法生成/写入 CSV
- javascript - 如何判断内联对象标签是否已加载其数据
- elm - 如何将文件加载到 Elm REPL?