Exemple #1
0
 def encrypt_auth_ack_message(self, ack_message: bytes) -> bytes:
     if self.got_eip8_auth:
         # The EIP-8 version has an authenticated length prefix.
         prefix = struct.pack('>H', len(ack_message) + ENCRYPT_OVERHEAD_LENGTH)
         suffix = ecies.encrypt(
             ack_message, self.remote.pubkey, shared_mac_data=prefix)
         auth_ack = prefix + suffix
     else:
         auth_ack = ecies.encrypt(ack_message, self.remote.pubkey)
     return auth_ack
Exemple #2
0
 def encrypt_auth_ack_message(self, ack_message: bytes) -> bytes:
     if self.got_eip8_auth:
         # The EIP-8 version has an authenticated length prefix.
         prefix = struct.pack('>H', len(ack_message) + ENCRYPT_OVERHEAD_LENGTH)
         suffix = ecies.encrypt(
             ack_message, self.remote.pubkey, shared_mac_data=prefix)
         auth_ack = prefix + suffix
     else:
         auth_ack = ecies.encrypt(ack_message, self.remote.pubkey)
     return auth_ack
Exemple #3
0
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)
Exemple #4
0
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)
Exemple #5
0
def _encrypt(public_key: bytes, msg: str) -> bytes:
    """Use ECIES to encrypt a message with a given public key and optional MAC.

    Args:
        public_key (bytes): The public_key to encrypt the message with.
        msg (str): The message to be encrypted.
    
    Returns:
        bytes: returns the cryptotext encrypted with the public key.

    >>> priv_key = "28e516f1e2f99e96a48a23cea1f94ee5f073403a1c68e818263f0eb898f1c8e5"
    >>> pub_key = b"2dbc2c2c86052702e7c219339514b2e8bd4687ba1236c478ad41b43330b08488c12c8c1797aa181f3a4596a1bd8a0c18344ea44d6655f61fa73e56e743f79e0d"
    >>> msg = "test"
    >>> _decrypt(priv_key, _encrypt(pub_key, msg)) == msg
    True
    """
    pub_key = keys.PublicKey(codecs.decode(public_key, 'hex'))
    msg_bytes = msg.encode(encoding='utf-8')
    return ecies.encrypt(msg_bytes, pub_key, shared_mac_data=SHARED_MAC_DATA)
Exemple #6
0
 def encrypt_auth_message(self, auth_message: bytes) -> bytes:
     return ecies.encrypt(auth_message, self.remote.pubkey)
Exemple #7
0
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
Exemple #8
0
 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
Exemple #9
0
def _encrypt(public_key: bytes, msg: str):
    pub_key = keys.PublicKey(codecs.decode(public_key, 'hex'))
    msg_bytes = msg.encode(encoding='utf-8')
    return ecies.encrypt(msg_bytes, pub_key, shared_mac_data=SHARED_MAC_DATA)
Exemple #10
0
 def encrypt_auth_message(self, auth_message: bytes) -> bytes:
     return ecies.encrypt(auth_message, self.remote.pubkey)