def new_deterministic(self, name, type, text): for i in range(0, 2**64, 2): s1 = serialize.Serializer() s1.str(text) s1.vint(i) sigkey = crypto.sha512(s1.data)[0:32] s2 = serialize.Serializer() s2.str(text) s2.vint(i + 1) deckey = crypto.sha512(s2.data)[0:32] verkey = crypto.priv_to_pub(sigkey) enckey = crypto.priv_to_pub(deckey) ripe = crypto.to_ripe(verkey, enckey) if ripe[0:1] == b'\x00': return self.new_identity(name, type, sigkey, deckey)
def to_bytes(self): s = serialize.Serializer() s.uint(self.magic, 4) s.bytes(bytes(self.command, 'utf8')[:12].ljust(12, b'\0')) s.uint(self.length, 4) s.bytes(self.checksum) return s.data
def encrypt(pubkey, data): public_key = _pub_to_public(pubkey) private_key = ec.generate_private_key(ec.SECP256K1(), openssl.backend) secret = private_key.exchange(ec.ECDH(), public_key) key = hashlib.sha512(secret).digest() enckey = key[0:32] mackey = key[32:64] iv = os.urandom(16) padder = padding.PKCS7(128).padder() paddeddata = padder.update(data) + padder.finalize() cipher = Cipher(algorithms.AES(enckey), modes.CBC(iv), openssl.backend) encryptor = cipher.encryptor() ciphertext = encryptor.update(paddeddata) + encryptor.finalize() s = serialize.Serializer() s.bytes(iv) s.uint(0x02ca, 2) public_numbers = private_key.public_key().public_numbers() x = public_numbers.x.to_bytes(32, 'big').lstrip(b'\x00') s.uint(len(x), 2) s.bytes(x) y = public_numbers.y.to_bytes(32, 'big').lstrip(b'\x00') s.uint(len(y), 2) s.bytes(y) s.bytes(ciphertext) maccer = HMAC(mackey, hashes.SHA256(), openssl.backend) maccer.update(s.data) mac = maccer.finalize() s.bytes(mac) return s.data
def data(self): s = serialize.Serializer() s.uint(self.nonce, 8) s.uint(self.expires, 8) s.uint(self.type, 4) s.vint(self.version) s.vint(self.stream) s.bytes(self.payload) return s.data
def to_bytes(self): s = serialize.Serializer() s.uint(self.behavior, 4) s.bytes(self.verkey) s.bytes(self.enckey) s.vint(self.trials) s.vint(self.extra) s.vbytes(self.signature) return s.data
def data(self): s = serialize.Serializer() s.vint(len(self.addresses)) for address in self.addresses: s.uint(address.time, 8) s.uint(address.stream, 4) s.uint(address.services, 8) s.bytes(address.ip) s.uint(address.port, 2) return s.data
def to_bytes(self): s = serialize.Serializer() s.vint(self.addrver) s.vint(self.stream) s.uint(self.behavior, 4) s.bytes(self.verkey) s.bytes(self.enckey) s.vint(self.trials) s.vint(self.extra) s.bytes(self.ripe) s.vint(self.encoding) s.vbytes(self.message) s.vbytes(self.ack) s.vbytes(self.signature) return s.data
def data(self): t = serialize.Serializer() t.uint(self.version, 4) t.uint(self.services, 8) t.uint(self.timestamp, 8) t.uint(self.dst_services, 8) t.bytes(self.dst_ip) t.uint(self.dst_port, 2) t.uint(self.src_services, 8) t.bytes(self.src_ip) t.uint(self.src_port, 2) t.uint(self.nonce, 8) t.vstr(self.user_agent) t.vint(len(self.streams)) for stream in self.streams: t.vint(stream) return t.data
def verify(self, object): s = serialize.Serializer() s.uint(object.expires, 8) s.uint(object.type, 4) s.vint(object.version) s.vint(object.stream) s.vint(self.addrver) s.vint(self.stream) s.uint(self.behavior, 4) s.bytes(self.verkey) s.bytes(self.enckey) s.vint(self.trials) s.vint(self.extra) s.bytes(self.ripe) s.vint(self.encoding) s.vbytes(self.message) s.vbytes(self.ack) crypto.verify(self.verkey, s.data, self.signature)
def data(self): s = serialize.Serializer() s.vint(len(self.hashes)) for hash in self.hashes: s.bytes(hash) return s.data
def to_bytes(self): s = serialize.Serializer() s.vint(self.version) s.vint(self.stream) s.bytes(self.ripe) return s.data
def to_str(self): s = serialize.Serializer() s.vint(self.version) s.vint(self.stream) s.bytes(self.ripe.lstrip(b'\x00')) return base58.encode(s.data)