Skip to content

Commit cbf5061

Browse files
Apply suggestions from code review
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
1 parent 03d0699 commit cbf5061

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

hivemind_bus_client/encryption.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,9 @@ def encrypt_bin(key: Union[str, bytes], data: Union[str, bytes], cipher: BinaryC
131131
Args:
132132
key (Union[str, bytes]): The encryption key, up to 16 bytes. Longer keys will be truncated.
133133
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
135137
136138
Returns:
137139
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],
203205
text = bytes(text, encoding="utf-8")
204206
if not isinstance(key, bytes):
205207
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")
206210
cipher = AES.new(key, AES.MODE_GCM, nonce=nonce)
207211
ciphertext, tag = cipher.encrypt_and_digest(text)
208212
return ciphertext, tag, cipher.nonce
@@ -234,7 +238,7 @@ def encrypt_ChaCha20_Poly1305(key: Union[str, bytes],
234238
text: Union[str, bytes],
235239
nonce: Optional[bytes] = None) -> tuple[bytes, bytes, bytes]:
236240
"""
237-
Encrypts plaintext using AES-GCM-128.
241+
Encrypts plaintext using ChaCha20-Poly1305.
238242
239243
Args:
240244
key (Union[str, bytes]): The encryption key. Strings will be encoded as UTF-8.

0 commit comments

Comments
 (0)