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