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:
parent
0007dc8690
commit
2fe2c9a643
@ -1,4 +1,5 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Linq;
|
||||||
using System.Security.Cryptography;
|
using System.Security.Cryptography;
|
||||||
|
|
||||||
namespace Teknik.Utilities.Cryptography
|
namespace Teknik.Utilities.Cryptography
|
||||||
@ -105,7 +106,7 @@ namespace Teknik.Utilities.Cryptography
|
|||||||
|
|
||||||
// Initialize Counter
|
// Initialize Counter
|
||||||
_Counter = new PooledArray(initialCounter.Length);
|
_Counter = new PooledArray(initialCounter.Length);
|
||||||
initialCounter.Array.CopyTo(_Counter.Array, 0);
|
initialCounter.CopyTo(_Counter.Array);
|
||||||
|
|
||||||
// Initialize the encrypted counter
|
// Initialize the encrypted counter
|
||||||
_EncryptedCounter = new PooledArray(_BlockSize / 8);
|
_EncryptedCounter = new PooledArray(_BlockSize / 8);
|
||||||
@ -208,7 +209,7 @@ namespace Teknik.Utilities.Cryptography
|
|||||||
|
|
||||||
public void ResetCounter()
|
public void ResetCounter()
|
||||||
{
|
{
|
||||||
_IV.Array.CopyTo(_Counter.Array, 0);
|
_IV.CopyTo(_Counter.Array);
|
||||||
_Iterations = 0;
|
_Iterations = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
@ -22,16 +23,18 @@ namespace Teknik.Utilities.Cryptography
|
|||||||
public AesCounterStream(Stream stream, bool encrypt, PooledArray key, PooledArray iv)
|
public AesCounterStream(Stream stream, bool encrypt, PooledArray key, PooledArray iv)
|
||||||
{
|
{
|
||||||
_Inner = stream;
|
_Inner = stream;
|
||||||
|
var keyBytes = key.ToArray();
|
||||||
|
var ivBytes = iv.ToArray();
|
||||||
|
|
||||||
// Create the Aes Cipher
|
// Create the Aes Cipher
|
||||||
using AesCounterMode aes = new AesCounterMode(iv);
|
using AesCounterMode aes = new AesCounterMode(iv);
|
||||||
if (encrypt)
|
if (encrypt)
|
||||||
{
|
{
|
||||||
_Cipher = (CounterModeCryptoTransform)aes.CreateEncryptor(key.Array, iv.Array); // Encrypt
|
_Cipher = (CounterModeCryptoTransform)aes.CreateEncryptor(keyBytes, ivBytes); // Encrypt
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
_Cipher = (CounterModeCryptoTransform)aes.CreateDecryptor(key.Array, iv.Array); // Decrypt
|
_Cipher = (CounterModeCryptoTransform)aes.CreateDecryptor(keyBytes, ivBytes); // Decrypt
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sync the counter
|
// Sync the counter
|
||||||
|
@ -28,6 +28,16 @@ namespace Teknik.Utilities
|
|||||||
Length = array.Length;
|
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()
|
public void Dispose()
|
||||||
{
|
{
|
||||||
_arrayPool.Return(Array);
|
_arrayPool.Return(Array);
|
||||||
|
Loading…
Reference in New Issue
Block a user