Created
October 28, 2017 04:38
-
-
Save zxexz/6751c9d19e2d7541897d4a868a692efc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from hashlib import sha256, sha1 | |
from uuid import uuid4 | |
from os import urandom | |
from Crypto.Cipher import AES | |
from binascii import b2a_base64 | |
from ctypes import c_int | |
import numpy as np | |
zero = np.uint32(0) | |
_joiner__ = ':'.encode() | |
class HashCashClient: | |
ver = 1 | |
bits = 15 | |
date = b'YYMMDD' | |
resource = b'test@localhost' | |
extension = str().encode() | |
rand = None | |
counter = None | |
solved = False | |
def __init__(self): | |
self.populate_counter() | |
self.populate_rand() | |
def get_str(self): | |
return _joiner__.join([str(self.ver).encode(), | |
str(self.bits).encode(), | |
self.date, | |
self.resource, | |
self.extension, | |
self.rand, | |
str(self.counter).encode()]) | |
def populate_rand(self): | |
self.rand = b2a_base64(urandom(20), newline=False) | |
def populate_counter(self): | |
self.counter = int.from_bytes(urandom(4), byteorder='big') | |
def solve(self): | |
str_repr = None | |
count = 0 | |
while not self.solved: | |
str_repr = self.get_str() | |
if np.right_shift(np.fromstring(sha1(str_repr).digest()[0:4], dtype='uint32')[0], 32-self.bits) == 0: | |
self.solved = True | |
print(count) | |
else: | |
self.counter += 1 | |
count += 1 | |
return str_repr | |
a = HashCashClient() | |
t = a.solve() | |
print(t) | |
def verify(s): | |
dat = {k:v.decode() for k, v in zip(['ver', 'bits', 'date', 'resource', 'extension', 'rand', 'counter'],s.split(b":"))} | |
bits = int(dat['bits']) | |
return True if np.right_shift(np.fromstring(sha1(s).digest()[0:4], dtype='uint32'), 32-bits) == 0 else False | |
verify(t) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment