def test_encrypt_decrypt(): msg = b"test yeah" privkey = ecies.generate_privkey() ciphertext = ecies.encrypt(msg, privkey.public_key) decrypted = ecies.decrypt(ciphertext, privkey) assert decrypted == msg privkey2 = ecies.generate_privkey() with pytest.raises(ecies.DecryptionError): decrypted = ecies.decrypt(ciphertext, privkey2)
def encrypt_eip8_msg(msg: bytes, pubkey: datatypes.PublicKey) -> bytes: prefix = struct.pack(">H", len(msg) + ENCRYPT_OVERHEAD_LENGTH) suffix = ecies.encrypt(msg, pubkey, shared_mac_data=prefix) return prefix + suffix
def encrypt_auth_ack_message(self, ack_message: bytes) -> bytes: if self.use_eip8: auth_ack = encrypt_eip8_msg(ack_message, self.remote.pubkey) else: auth_ack = ecies.encrypt(ack_message, self.remote.pubkey) return auth_ack