@@ -164,7 +164,7 @@ impl AlgorithmProcess for ChaCha20 {
164164 /// for both encryption and decryption due to the reversible nature of the XOR operation.
165165 ///
166166 /// # Arguments
167- /// * `bytes_in` - A slice of bytes representing the input data to be processed (either plaintext for encryption
167+ /// * `bytes_in` - A mutable vector of bytes representing the input data to be processed (either plaintext for encryption
168168 /// or ciphertext for decryption).
169169 ///
170170 /// # Returns
@@ -185,12 +185,9 @@ impl AlgorithmProcess for ChaCha20 {
185185 /// # Notes
186186 /// It's important to use the same nonce and key for decrypting the data that were used for encryption.
187187 /// The output size will be equal to the input size, as ChaCha20 is a stream cipher.
188- fn process ( & mut self , bytes_in : & [ u8 ] ) -> Vec < u8 > {
189- // Clone the input bytes to prepare the output vector
190- let mut out = bytes_in. to_owned ( ) ;
191-
188+ fn process ( & mut self , mut bytes_in : Vec < u8 > ) -> Vec < u8 > {
192189 // Process each 64-byte block of the input data
193- out
190+ bytes_in
194191 . par_chunks_mut ( CHACHA20_BLOCK_SIZE * 100 )
195192 . enumerate ( )
196193 . for_each ( |( i, par_chunk) | {
@@ -203,7 +200,7 @@ impl AlgorithmProcess for ChaCha20 {
203200 } ) ;
204201
205202 // Return the processed data
206- out . to_vec ( )
203+ bytes_in
207204 }
208205}
209206
@@ -310,7 +307,7 @@ mod tests {
310307 ] ,
311308 ) ;
312309
313- let encrypted_data = chacha20. process ( & PLAINTEXT ) ;
310+ let encrypted_data = chacha20. process ( PLAINTEXT . to_vec ( ) ) ;
314311
315312 assert_eq ! ( encrypted_data, CIPHERTEXT ) ;
316313 }
@@ -319,10 +316,10 @@ mod tests {
319316 fn it_can_reverse_encryption ( ) {
320317 let mut chacha20 = ChaCha20 :: default ( ) ;
321318 chacha20. init ( & [ 1u8 ; 32 ] , & [ 2u8 ; CHACHA20_NONCE_SIZE ] ) ;
322- let data = [ 0u8 ; 64 ] ;
319+ let data = vec ! [ 0u8 ; 64 ] ;
323320
324- let encrypted_data = chacha20. process ( & data) ;
325- let decrypted_data = chacha20. process ( & encrypted_data) ;
321+ let encrypted_data = chacha20. process ( data. clone ( ) ) ;
322+ let decrypted_data = chacha20. process ( encrypted_data) ;
326323
327324 assert_eq ! ( decrypted_data, data) ;
328325 }
@@ -331,10 +328,10 @@ mod tests {
331328 fn it_can_reverse_encryption_for_data_smaller_than_a_chunk ( ) {
332329 let mut chacha20 = ChaCha20 :: default ( ) ;
333330 chacha20. init ( & [ 1u8 ; 32 ] , & [ 2u8 ; CHACHA20_NONCE_SIZE ] ) ;
334- let data = [ 0u8 ; 1 ] ;
331+ let data = vec ! [ 0u8 ; 1 ] ;
335332
336- let encrypted_data = chacha20. process ( & data) ;
337- let decrypted_data = chacha20. process ( & encrypted_data) ;
333+ let encrypted_data = chacha20. process ( data. clone ( ) ) ;
334+ let decrypted_data = chacha20. process ( encrypted_data) ;
338335
339336 assert_eq ! ( decrypted_data, data) ;
340337 }
@@ -345,11 +342,11 @@ mod tests {
345342 chacha20_1. init ( & [ 0u8 ; 32 ] , & [ 0u8 ; CHACHA20_NONCE_SIZE ] ) ;
346343 let mut chacha20_2 = ChaCha20 :: default ( ) ;
347344 chacha20_2. init ( & [ 0u8 ; 32 ] , & [ 0u8 ; CHACHA20_NONCE_SIZE ] ) ;
348- let mut data = [ 0u8 ; 64 * 1000 ] ;
345+ let mut data = vec ! [ 0u8 ; 64 * 1000 ] ;
349346 let data2 = data. clone ( ) ;
350347
351348 chacha20_1. process_in_place ( & mut data) ;
352- let encrypted_sync = chacha20_2. process ( & data2) ;
349+ let encrypted_sync = chacha20_2. process ( data2) ;
353350
354351 assert_eq ! ( data. to_vec( ) , encrypted_sync) ;
355352 }
0 commit comments