@@ -131,7 +131,9 @@ def encrypt_bin(key: Union[str, bytes], data: Union[str, bytes], cipher: BinaryC
131
131
Args:
132
132
key (Union[str, bytes]): The encryption key, up to 16 bytes. Longer keys will be truncated.
133
133
data (Union[str, bytes]): The data to encrypt. Strings will be encoded as UTF-8.
134
- cipher (BinaryCiphers): The encryption cipher. Only BINARY_AES_GCM_128 is supported.
134
+ cipher (BinaryCiphers): The encryption cipher. Supported options:
135
+ - BINARY_AES_GCM_128: AES-GCM with 128-bit key
136
+ - BINARY_CHACHA20_POLY1305: ChaCha20-Poly1305 with 256-bit key
135
137
136
138
Returns:
137
139
bytes: The encrypted data, including the nonce and tag.
@@ -203,6 +205,8 @@ def encrypt_AES_GCM_128(key: Union[str, bytes], text: Union[str, bytes],
203
205
text = bytes (text , encoding = "utf-8" )
204
206
if not isinstance (key , bytes ):
205
207
key = bytes (key , encoding = "utf-8" )
208
+ if len (key ) != 16 : # AES-128 uses 128 bit/16 byte keys
209
+ raise ValueError ("AES-GCM-128 requires a 16-byte key" )
206
210
cipher = AES .new (key , AES .MODE_GCM , nonce = nonce )
207
211
ciphertext , tag = cipher .encrypt_and_digest (text )
208
212
return ciphertext , tag , cipher .nonce
@@ -234,7 +238,7 @@ def encrypt_ChaCha20_Poly1305(key: Union[str, bytes],
234
238
text : Union [str , bytes ],
235
239
nonce : Optional [bytes ] = None ) -> tuple [bytes , bytes , bytes ]:
236
240
"""
237
- Encrypts plaintext using AES-GCM-128 .
241
+ Encrypts plaintext using ChaCha20-Poly1305 .
238
242
239
243
Args:
240
244
key (Union[str, bytes]): The encryption key. Strings will be encoded as UTF-8.
0 commit comments