1
0
mirror of https://git.teknik.io/Teknikode/Teknik.git synced 2023-08-02 14:16:22 +02:00

Fixed pool array usage

This commit is contained in:
Uncled1023 2022-05-28 21:56:41 -07:00
parent 0007dc8690
commit 2fe2c9a643
3 changed files with 18 additions and 4 deletions

View File

@ -1,4 +1,5 @@
using System;
using System.Linq;
using System.Security.Cryptography;
namespace Teknik.Utilities.Cryptography
@ -105,7 +106,7 @@ namespace Teknik.Utilities.Cryptography
// Initialize Counter
_Counter = new PooledArray(initialCounter.Length);
initialCounter.Array.CopyTo(_Counter.Array, 0);
initialCounter.CopyTo(_Counter.Array);
// Initialize the encrypted counter
_EncryptedCounter = new PooledArray(_BlockSize / 8);
@ -208,7 +209,7 @@ namespace Teknik.Utilities.Cryptography
public void ResetCounter()
{
_IV.Array.CopyTo(_Counter.Array, 0);
_IV.CopyTo(_Counter.Array);
_Iterations = 0;
}

View File

@ -1,5 +1,6 @@
using System;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
@ -22,16 +23,18 @@ namespace Teknik.Utilities.Cryptography
public AesCounterStream(Stream stream, bool encrypt, PooledArray key, PooledArray iv)
{
_Inner = stream;
var keyBytes = key.ToArray();
var ivBytes = iv.ToArray();
// Create the Aes Cipher
using AesCounterMode aes = new AesCounterMode(iv);
if (encrypt)
{
_Cipher = (CounterModeCryptoTransform)aes.CreateEncryptor(key.Array, iv.Array); // Encrypt
_Cipher = (CounterModeCryptoTransform)aes.CreateEncryptor(keyBytes, ivBytes); // Encrypt
}
else
{
_Cipher = (CounterModeCryptoTransform)aes.CreateDecryptor(key.Array, iv.Array); // Decrypt
_Cipher = (CounterModeCryptoTransform)aes.CreateDecryptor(keyBytes, ivBytes); // Decrypt
}
// Sync the counter

View File

@ -28,6 +28,16 @@ namespace Teknik.Utilities
Length = array.Length;
}
public void CopyTo(byte[] destination)
{
System.Array.Copy(Array, destination, Length);
}
public byte[] ToArray()
{
return Array.Take(Length).ToArray();
}
public void Dispose()
{
_arrayPool.Return(Array);