Skip to content

Commit 4b663fb

Browse files
Implement __new__() constructor with key (32 bytes) and nonce (12 bytes) length checks
1 parent 3b20043 commit 4b663fb

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

crypto/ChaCha20.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,14 @@ class ChaCha20:
1414
12-byte (96-bit) nonce.
1515
counter : int
1616
32-bit counter, typically starts at 0.
17-
"""
17+
"""
18+
def __new__(cls, key: bytes, nonce: bytes, counter: int = 0):
19+
if not isinstance(key, bytes) or len(key) != 32:
20+
raise ValueError("Key must be exactly 32 bytes (256 bits).")
21+
if not isinstance(nonce, bytes) or len(nonce) != 12:
22+
raise ValueError("Nonce must be exactly 12 bytes (96 bits).")
23+
instance = super().__new__(cls)
24+
instance.key = key
25+
instance.nonce = nonce
26+
instance.counter = counter
27+
return instance

0 commit comments

Comments
 (0)