def __init__(self, version, stream, ripe): self.version = version self.stream = stream self.ripe = ripe sha = crypto.sha512d(self.to_bytes()) self.xkey = sha[0:32] self.tag = sha[32:64]
def brink(self): a = self.nonce.to_bytes(8, 'big') initial = crypto.sha512(self.data[8:]) c = crypto.sha512d(a + initial) value = int.from_bytes(c[:8], 'big') return int(self.expires - 2**80 / (value * config.NETWORK_TRIALS * (len(self.data) + config.NETWORK_EXTRA)) + 2**16)
def decode(chars): chars = chars.strip() if chars[:3] == 'BM-': chars = chars[3:] data = decode_raw(chars) checksum = crypto.sha512d(data[:-4])[:4] assert checksum == data[-4:] return data[:-4]
def check(payload, trials, extra, ttl, nonce): length = len(payload) + 8 + extra target = int(2**64/(trials*(length+ttl*length/(2**16)))) initial = crypto.sha512(payload) a = nonce.to_bytes(8, 'big') c = crypto.sha512d(a+initial) value = int.from_bytes(c[:8], 'big') return value <= target
def pow(payload, trials, extra, ttl): length = len(payload) + 8 + extra target = int(2**64/(trials*(length+max(ttl,0)*length/(2**16)))) value = target + 1 initial = crypto.sha512(payload) nonce = int.from_bytes(os.urandom(8), 'big') # Make it harder for attackers to determine how many numbers we have tried while value > target: nonce = (nonce + 1) % (2**64) a = nonce.to_bytes(8, 'big') c = crypto.sha512d(a+initial) value = int.from_bytes(c[:8], 'big') return nonce
def hash(self): return crypto.sha512d(self.data)[:32]
def encode(data, prepend_bm=False): data += crypto.sha512d(data)[:4] text = encode_raw(data) if prepend_bm: text = 'BM-' + text return text
def encode(data, prepend_bm=False): data += crypto.sha512d(data)[:4] text = encode_raw(data) if prepend_bm: text = 'BM-'+text return text
def brink(self): a = self.nonce.to_bytes(8, 'big') initial = crypto.sha512(self.data[8:]) c = crypto.sha512d(a+initial) value = int.from_bytes(c[:8], 'big') return int(self.expires-2**80/(value*config.NETWORK_TRIALS*(len(self.data)+config.NETWORK_EXTRA))+2**16)