5252BLOB_ID_LENGTH = 16
5353
5454
55- # TODO: Raises?
5655def _pk_encrypt (key_pair : Tuple [Key , Key ], data : bytes , nonce : bytes = None ):
5756 """
5857 Encrypt data by using public-key encryption.
@@ -63,6 +62,9 @@ def _pk_encrypt(key_pair: Tuple[Key, Key], data: bytes, nonce: bytes = None):
6362 - `data`: Raw data.
6463 - `nonce`: A predefined nonce.
6564
65+ Raises `libnacl.CryptError` in case the data could not be encrypted.
66+ Raises `ValueError` in other cases.
67+
6668 Return a tuple of bytes containing the nonce and the encrypted
6769 data.
6870 """
@@ -72,7 +74,6 @@ def _pk_encrypt(key_pair: Tuple[Key, Key], data: bytes, nonce: bytes = None):
7274 return box .encrypt (data , nonce = nonce , pack_nonce = False )
7375
7476
75- # TODO: Raises?
7677def _pk_decrypt (key_pair : Tuple [Key , Key ], nonce : bytes , data : bytes ):
7778 """
7879 Decrypt data by using public-key decryption.
@@ -83,6 +84,9 @@ def _pk_decrypt(key_pair: Tuple[Key, Key], nonce: bytes, data: bytes):
8384 - `nonce`: The nonce of the encrypted data.
8485 - `data`: Encrypted data.
8586
87+ Raises `libnacl.CryptError` in case the data could not be decrypted.
88+ Raises `ValueError` in other cases.
89+
8690 Return the decrypted data.
8791 """
8892 # Decrypt payload
@@ -650,7 +654,7 @@ def encrypt(self, data, key_pair=None, nonce=None):
650654 # Encrypt
651655 try :
652656 return _pk_encrypt (key_pair , data , nonce = nonce )
653- except ValueError as exc :
657+ except ( ValueError , libnacl . CryptError ) as exc :
654658 raise MessageError ('Could not encrypt data' ) from exc
655659
656660 @classmethod
@@ -671,8 +675,8 @@ def decrypt(cls, nonce, data, key_pair):
671675 # Decrypt
672676 try :
673677 return _pk_decrypt (key_pair , nonce , data )
674- except ValueError :
675- raise MessageError ('Could not decrypt data' )
678+ except ( ValueError , libnacl . CryptError ) as exc :
679+ raise MessageError ('Could not decrypt data' ) from exc
676680
677681
678682# TODO: Update docstring (arguments)
0 commit comments