proofow/proofow.py

19 lines
511 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:
2017-12-01 19:45:52 +00:00
def __init__(self, difficulty, base=None):
self._base = urandom(32) if not base else base
2017-12-01 19:25:57 +00:00
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