1111
1212def encrypt_asymmetric (
1313 plaintext : Union [Buffer , str ],
14- public_key : Union [Buffer , Base64String ],
15- private_key : Union [Buffer , Base64String ],
14+ public_key : Union [Buffer , Base64String , public . PublicKey ],
15+ private_key : Union [Buffer , Base64String , public . PrivateKey ],
1616) -> Tuple [Base64String , Base64String ]:
1717 """Performs asymmetric encryption of the ``plaintext`` with x25519-xsalsa20-poly1305
1818 algorithm with the given parameters. Each of those params should be either the raw value in bytes
@@ -24,21 +24,33 @@ def encrypt_asymmetric(
2424 :raises ValueError: If ``plaintext``, ``public_key`` or ``private_key`` are empty
2525 :return: A tuple containing the ciphered text and the random nonce used for encryption
2626 """
27- if len (plaintext ) == 0 or len (public_key ) == 0 or len (private_key ) == 0 :
28- raise ValueError ()
27+ if (not isinstance (public_key , public .PublicKey ) and len (public_key ) == 0 ) or (
28+ not isinstance (private_key , public .PrivateKey ) and len (private_key ) == 0
29+ ):
30+ raise ValueError ("Public key and private key cannot be empty!" )
2931
3032 m_plaintext = (
3133 str .encode (plaintext , "utf-8" ) if isinstance (plaintext , str ) else plaintext
3234 )
3335 m_public_key = (
3436 b64decode (public_key ) if isinstance (public_key , Base64String ) else public_key
3537 )
38+ m_public_key = (
39+ public .PublicKey (m_public_key )
40+ if isinstance (m_public_key , (bytes , bytearray , memoryview ))
41+ else m_public_key
42+ )
3643 m_private_key = (
3744 b64decode (private_key ) if isinstance (private_key , Base64String ) else private_key
3845 )
46+ m_private_key = (
47+ public .PrivateKey (m_private_key )
48+ if isinstance (m_private_key , (bytes , bytearray , memoryview ))
49+ else m_private_key
50+ )
3951
4052 nonce = utils .random (24 )
41- box = public .Box (public . PrivateKey ( m_private_key ), public . PublicKey ( m_public_key ) )
53+ box = public .Box (m_private_key , m_public_key )
4254 ciphertext = box .encrypt (m_plaintext , nonce ).ciphertext
4355
4456 return (b64encode (ciphertext ).decode ("utf-8" ), b64encode (nonce ).decode ("utf-8" ))
@@ -67,7 +79,9 @@ def decrypt_asymmetric(
6779 or (not isinstance (public_key , public .PublicKey ) and len (public_key ) == 0 )
6880 or (not isinstance (private_key , public .PrivateKey ) and len (private_key ) == 0 )
6981 ):
70- raise ValueError ()
82+ raise ValueError (
83+ "Public key, private key, ciphertext and nonce cannot be empty!"
84+ )
7185
7286 m_ciphertext = (
7387 b64decode (ciphertext ) if isinstance (ciphertext , Base64String ) else ciphertext
@@ -107,8 +121,8 @@ def encrypt_symmetric(
107121 :raises ValueError: If either ``plaintext`` or ``key`` is empty
108122 :return: Ciphered text
109123 """
110- if len (plaintext ) == 0 or len ( key ) == 0 :
111- raise ValueError ()
124+ if len (key ) == 0 :
125+ raise ValueError ("The given key is empty!" )
112126
113127 BLOCK_SIZE_BYTES = 16
114128
@@ -146,7 +160,7 @@ def decrypt_symmetric(
146160 :raises ValueError: If ``ciphertext``, ``iv``, ``tag`` or ``key`` are empty or tag/mac does not match
147161 :return: Deciphered text
148162 """
149- if len (ciphertext ) == 0 or len ( tag ) == 0 or len (iv ) == 0 or len (key ) == 0 :
163+ if len (tag ) == 0 or len (iv ) == 0 or len (key ) == 0 :
150164 raise ValueError ("One of the given parameter is empty!" )
151165
152166 m_key = b64decode (key ) if isinstance (key , Base64String ) else key
0 commit comments