32 lines
715 B
Python
32 lines
715 B
Python
from os import urandom
|
|
from itertools import count
|
|
from hashlib import sha256
|
|
|
|
|
|
|
|
class pow_base:
|
|
def __init__(self, difficulty, base=None):
|
|
self._base = urandom(self._bits//8) if not base else base
|
|
self._target = 2 ** (self._bits - difficulty)
|
|
|
|
def work(self):
|
|
for nonce in count():
|
|
if self.verify(nonce):
|
|
return nonce
|
|
|
|
def verify(self, nonce):
|
|
hexresult = self._hexdigest(self._base + str(nonce).encode())
|
|
return int(hexresult, 16) < self._target
|
|
|
|
|
|
|
|
class pow_hashlib_base(pow_base):
|
|
def _hexdigest(self, data):
|
|
return self._hasher(data).hexdigest()
|
|
|
|
|
|
|
|
class pow256(pow_hashlib_base):
|
|
_bits = 256
|
|
_hasher = sha256
|