1- using SAPTeam . Kryptor . CryptoProviders ;
1+ using System . Runtime . CompilerServices ;
2+
3+ using SAPTeam . Kryptor . CryptoProviders ;
24using SAPTeam . Kryptor . Extensions ;
35using SAPTeam . Kryptor . Helpers ;
46
@@ -143,14 +145,26 @@ public virtual async Task<byte[]> EncryptBlockAsync(byte[] data, CryptoProcess p
143145 process . ChunkIndex = 0 ;
144146 }
145147
146- process . BlockHash = Configuration . RemoveHash ? Array . Empty < byte > ( ) : data . Sha256 ( ) ;
148+ process . BlockHash = Configuration . RemoveHash ? [ ] : data. Sha256 ( ) ;
147149 byte [ ] hash = Configuration . DynamicBlockProcessing ? Transformers . Rotate ( process . BlockHash , DynamicEncryption . GetDynamicBlockEntropy ( KeyStore , process ) ) : process . BlockHash ;
148- List < byte > result = new List < byte > ( hash ) ;
150+
151+ // Pre-calculate capacity: hash + (chunks * chunk size)
152+ int estimatedChunks = ( data . Length + EncryptionChunkSize - 1 ) / EncryptionChunkSize ;
153+ List < byte > result = new List < byte > ( hash . Length + ( estimatedChunks * DecryptionChunkSize ) ) ;
154+ result . AddRange ( hash ) ;
149155
150156 foreach ( byte [ ] chunk in data . Chunk ( EncryptionChunkSize ) )
151157 {
152158 IEnumerable < byte > c = await EncryptChunkAsync ( chunk , process , cancellationToken ) ;
153- result . AddRange ( Configuration . DynamicBlockProcessing ? Transformers . Rotate ( c . ToArray ( ) , DynamicEncryption . GetDynamicChunkEntropy ( KeyStore , process ) ) : c ) ;
159+ if ( Configuration . DynamicBlockProcessing )
160+ {
161+ byte [ ] cArray = c as byte [ ] ?? c . ToArray ( ) ;
162+ result . AddRange ( Transformers . Rotate ( cArray , DynamicEncryption . GetDynamicChunkEntropy ( KeyStore , process ) ) ) ;
163+ }
164+ else
165+ {
166+ result . AddRange ( c ) ;
167+ }
154168 process . ChunkIndex ++ ;
155169 }
156170
@@ -177,24 +191,39 @@ public virtual async Task<byte[]> DecryptBlockAsync(byte[] data, CryptoProcess p
177191 process . ChunkIndex = 0 ;
178192 }
179193
180- IEnumerable < byte [ ] > chunks ;
181-
182- if ( Configuration . RemoveHash )
194+ int dataOffset = 0 ;
195+
196+ if ( ! Configuration . RemoveHash )
183197 {
184- chunks = data . Chunk ( DecryptionChunkSize ) ;
185- }
186- else
187- {
188- byte [ ] _hash = data . Take ( 32 ) . ToArray ( ) ;
189- process . BlockHash = Configuration . DynamicBlockProcessing ? Transformers . Rotate ( _hash , DynamicEncryption . GetDynamicBlockEntropy ( KeyStore , process ) * - 1 ) : _hash ;
190- chunks = data . Skip ( 32 ) . Chunk ( DecryptionChunkSize ) ;
198+ // Extract hash without allocating via Take
199+ process . BlockHash = new byte [ 32 ] ;
200+ Array . Copy ( data , 0 , process . BlockHash , 0 , 32 ) ;
201+
202+ if ( Configuration . DynamicBlockProcessing )
203+ {
204+ process . BlockHash = Transformers . Rotate ( process . BlockHash , DynamicEncryption . GetDynamicBlockEntropy ( KeyStore , process ) * - 1 ) ;
205+ }
206+ dataOffset = 32 ;
191207 }
192208
193- List < byte > result = new List < byte > ( ) ;
209+ // Pre-calculate capacity for result list
210+ int estimatedChunks = ( data . Length - dataOffset + DecryptionChunkSize - 1 ) / DecryptionChunkSize ;
211+ List < byte > result = new List < byte > ( estimatedChunks * EncryptionChunkSize ) ;
194212
195- foreach ( byte [ ] chunk in chunks )
213+ // Process chunks without Skip/Chunk allocation overhead
214+ int dataLength = data . Length - dataOffset ;
215+ for ( int i = 0 ; i < dataLength ; i += DecryptionChunkSize )
196216 {
197- result . AddRange ( await DecryptChunkAsync ( Configuration . DynamicBlockProcessing ? Transformers . Rotate ( chunk , DynamicEncryption . GetDynamicChunkEntropy ( KeyStore , process ) * - 1 ) : chunk , process , cancellationToken ) ) ;
217+ int chunkLength = Math . Min ( DecryptionChunkSize , dataLength - i ) ;
218+ byte [ ] chunk = new byte [ chunkLength ] ;
219+ Array . Copy ( data , dataOffset + i , chunk , 0 , chunkLength ) ;
220+
221+ if ( Configuration . DynamicBlockProcessing )
222+ {
223+ chunk = Transformers . Rotate ( chunk , DynamicEncryption . GetDynamicChunkEntropy ( KeyStore , process ) * - 1 ) ;
224+ }
225+
226+ result . AddRange ( await DecryptChunkAsync ( chunk , process , cancellationToken ) ) ;
198227 process . ChunkIndex ++ ;
199228 }
200229
0 commit comments