1. 程式人生 > >簡易pow工作量證明機制機制實現

簡易pow工作量證明機制機制實現

from hashlib import sha256

#pow 工作量證明機制
x = 1
y = 2

# f'{x*y}'  x*y 的結果轉換成字串
# encode 預設是utf-8
#a = sha256(f'{x*y}'.encode()).hexdigest()[-5:]
# a ==b的時候算出一個hash是00000結尾的,那麼就拿到這個區塊
b = "00000"

while sha256(f'{x*y}'.encode()).hexdigest()[-5:] != b:
    y += 1
    print(y,sha256(f'{x*y}'.encode()).hexdigest()[-5
:]) print("y={}時刻,y求解".format(y))