66class ChaCha20 :
77 """
88 Implementation of the ChaCha20 stream cipher.
9-
9+
1010 Attributes
1111 ----------
1212 key : bytes
@@ -28,7 +28,7 @@ def __new__(cls, key: bytes, nonce: bytes, counter: int = 0):
2828 instance .nonce = nonce
2929 instance .counter = counter
3030 return instance
31-
31+
3232 def __init__ (self , key : bytes , nonce : bytes , counter : int = 0 ):
3333 """Initializes the ChaCha20 object."""
3434 # Guard against multiple initializations
@@ -81,25 +81,25 @@ def _chacha20_block(self, counter: int) -> bytes:
8181 self ._double_round (working_state )
8282 final_state = (working_state + state ) % (2 ** 32 )
8383 return struct .pack ('<16I' , * final_state .flatten ())
84-
84+
8585 def _apply_keystream (self , data : bytes ) -> bytes :
8686 """
87- Applies the ChaCha20 keystream to the input data (plaintext or ciphertext)
87+ Applies the ChaCha20 keystream to the input data (plaintext or ciphertext)
8888 to perform encryption or decryption.
8989
9090 This method processes the input data in 64-byte blocks. For each block:
9191 - A 64-byte keystream is generated using the `_chacha20_block()` function.
9292 - Each byte of the input block is XORed with the corresponding keystream byte.
9393 - The XORed result is appended to the output.
9494
95- The same function is used for both encryption and decryption because
95+ The same function is used for both encryption and decryption because
9696 XORing the ciphertext with the same keystream returns the original plaintext.
9797
9898 Args:
9999 data (bytes): The input data to be encrypted or decrypted (plaintext or ciphertext).
100100
101101 Returns:
102- bytes: The result of XORing the input data with the ChaCha20 keystream
102+ bytes: The result of XORing the input data with the ChaCha20 keystream
103103 (ciphertext if plaintext was provided, plaintext if ciphertext was provided).
104104 """
105105 if len (data ) == 0 :
@@ -123,7 +123,7 @@ def encrypt(self, plaintext: bytes) -> bytes:
123123 """
124124 Encrypts the given plaintext using the ChaCha20 stream cipher.
125125
126- This method uses the ChaCha20 keystream generated from the
126+ This method uses the ChaCha20 keystream generated from the
127127 key, nonce, and counter to XOR with the plaintext, producing ciphertext.
128128
129129 Args:
@@ -133,12 +133,12 @@ def encrypt(self, plaintext: bytes) -> bytes:
133133 bytes: The resulting ciphertext.
134134 """
135135 return self ._apply_keystream (plaintext )
136-
136+
137137 def decrypt (self , ciphertext : bytes ) -> bytes :
138138 """
139139 Decrypts the given ciphertext using the ChaCha20 stream cipher.
140140
141- Since ChaCha20 uses XOR for encryption, decryption is performed
141+ Since ChaCha20 uses XOR for encryption, decryption is performed
142142 using the same keystream and XOR operation.
143143
144144 Args:
@@ -148,9 +148,9 @@ def decrypt(self, ciphertext: bytes) -> bytes:
148148 bytes: The resulting plaintext.
149149 """
150150 return self .apply_keystream (ciphertext )
151-
151+
152152 def reset (self , counter : int = 0 ):
153153 """Resets the ChaCha20 counter to the specified value (default is 0)."""
154154 if not isinstance (counter , int ) or counter < 0 :
155155 raise ValueError ("Counter must be a non-negative integer." )
156- self .counter = counter
156+ self .counter = counter
0 commit comments