proofow/proofow.py

19 lines
478 B
Python
Raw Normal View History

2017-12-01 19:25:57 +00:00
from os import urandom
from itertools import count
from hashlib import sha256
class proofow:
def __init__(self, difficulty):
self._base = urandom(32)
self._target = 2 ** (256 - difficulty)
def work(self):
for nonce in count():
if self.verify(nonce):
return nonce
def verify(self, nonce):
hexresult = sha256(self._base + str(nonce).encode()).hexdigest()
return int(hexresult, 16) < self._target