From 860b932612d94caefe093defd80dc6e8b498ebf4 Mon Sep 17 00:00:00 2001 From: Simon Hughes Date: Fri, 18 Oct 2019 12:43:36 +0100 Subject: [PATCH] Add ability to specify KeySize and BlockSize --- Bytes.cs | 134 ++++++++++-- Digest.cs | 3 +- .../ByteTests.cs | 207 ++++++++++++------ .../HashTests.cs | 18 +- .../StringsTest.cs | 6 +- 5 files changed, 269 insertions(+), 99 deletions(-) diff --git a/Bytes.cs b/Bytes.cs index c0bf864..a41cf36 100644 --- a/Bytes.cs +++ b/Bytes.cs @@ -33,12 +33,22 @@ namespace Effortless.Net.Encryption { public static class Bytes { + public static int BufferLen = 4096; private static readonly RNGCryptoServiceProvider Rng = new RNGCryptoServiceProvider(); private static PaddingMode _paddingMode = PaddingMode.ISO10126; private static CipherMode _cipherMode = CipherMode.CBC; + public enum BlockSize + { + Default = 256, + Size128 = 128, + Size192 = 192, + Size256 = 256 + } + public enum KeySize { + Default = 256, Size128 = 128, Size192 = 192, Size256 = 256 @@ -67,14 +77,14 @@ public static bool SetPaddingAndCipherModes(PaddingMode paddingMode, CipherMode return true; } - private static RijndaelManaged GetRijndaelManaged(byte[] key, byte[] iv) + private static RijndaelManaged GetRijndaelManaged(byte[] key, byte[] iv, KeySize keySize, BlockSize blockSize) { var rm = new RijndaelManaged { - KeySize = 256, - BlockSize = 256, - Padding = _paddingMode, - Mode = _cipherMode + KeySize = (int) keySize, + BlockSize = (int) blockSize, + Padding = _paddingMode, + Mode = _cipherMode }; if (key != null) @@ -91,7 +101,15 @@ private static RijndaelManaged GetRijndaelManaged(byte[] key, byte[] iv) /// public static byte[] GenerateKey() { - using (var rm = GetRijndaelManaged(null, null)) + return GenerateKey(KeySize.Default, BlockSize.Default); + } + + /// + /// Returns an encryption key to be used with the Rijndael algorithm + /// + public static byte[] GenerateKey(KeySize keySize, BlockSize blockSize) + { + using (var rm = GetRijndaelManaged(null, null, keySize, blockSize)) { rm.GenerateKey(); return rm.Key; @@ -123,7 +141,15 @@ public static byte[] GenerateKey(string password, string salt, KeySize keySize, /// public static byte[] GenerateIV() { - using (var rm = GetRijndaelManaged(null, null)) + return GenerateIV(KeySize.Default, BlockSize.Default); + } + + /// + /// Returns the encryption IV to be used with the Rijndael algorithm + /// + public static byte[] GenerateIV(KeySize keySize, BlockSize blockSize) + { + using (var rm = GetRijndaelManaged(null, null, keySize, blockSize)) { rm.GenerateIV(); return rm.IV; @@ -134,6 +160,14 @@ public static byte[] GenerateIV() /// Encrypt a byte array into a byte array using the given Key and an IV /// public static byte[] Encrypt(byte[] clearData, byte[] key, byte[] iv) + { + return Encrypt(clearData, key, iv, KeySize.Default, BlockSize.Default); + } + + /// + /// Encrypt a byte array into a byte array using the given Key and an IV + /// + public static byte[] Encrypt(byte[] clearData, byte[] key, byte[] iv, KeySize keySize, BlockSize blockSize) { if (clearData == null || clearData.Length <= 0) throw new ArgumentNullException(nameof(clearData)); if (key == null || key.Length <= 0) throw new ArgumentNullException(nameof(key)); @@ -146,7 +180,7 @@ public static byte[] Encrypt(byte[] clearData, byte[] key, byte[] iv) // We are going to use Rijndael because it is strong and available on all platforms. // You can use other algorithms, to do so substitute the next line with something like // TripleDES alg = TripleDES.Create(); - using (var alg = GetRijndaelManaged(key, iv)) + using (var alg = GetRijndaelManaged(key, iv, keySize, blockSize)) { // Create a CryptoStream through which we are going to be pumping our data. // CryptoStreamMode.Write means that we are going to be writing data to the stream and the @@ -189,13 +223,12 @@ public static void Encrypt(Stream clearStreamIn, string encryptedFileOut, Rijnda { // Now will will initialize a buffer and will be processing the input file in chunks. // This is done to avoid reading the whole file (which can be huge) into memory. - const int bufferLen = 4096; - var buffer = new byte[bufferLen]; + var buffer = new byte[BufferLen]; int bytesRead; do { - bytesRead = clearStreamIn.Read(buffer, 0, bufferLen); // Read a chunk of data from the input file + bytesRead = clearStreamIn.Read(buffer, 0, BufferLen); // Read a chunk of data from the input file if (bytesRead > 0) cs.Write(buffer, 0, bytesRead); // Encrypt it } while (bytesRead != 0); @@ -212,6 +245,14 @@ public static void Encrypt(Stream clearStreamIn, string encryptedFileOut, Rijnda /// Encrypt a file into another file /// public static void Encrypt(string clearFileIn, string encryptedFileOut, byte[] key, byte[] iv) + { + Encrypt(clearFileIn, encryptedFileOut, key, iv, KeySize.Default, BlockSize.Default); + } + + /// + /// Encrypt a file into another file + /// + public static void Encrypt(string clearFileIn, string encryptedFileOut, byte[] key, byte[] iv, KeySize keySize, BlockSize blockSize) { if (string.IsNullOrEmpty(clearFileIn)) throw new ArgumentNullException(nameof(clearFileIn)); if (string.IsNullOrEmpty(encryptedFileOut)) throw new ArgumentNullException(nameof(encryptedFileOut)); @@ -219,7 +260,7 @@ public static void Encrypt(string clearFileIn, string encryptedFileOut, byte[] k if (key == null || key.Length <= 0) throw new ArgumentNullException(nameof(key)); if (iv == null || iv.Length <= 0) throw new ArgumentNullException(nameof(iv)); - using (var alg = GetRijndaelManaged(key, iv)) + using (var alg = GetRijndaelManaged(key, iv, keySize, blockSize)) { using (var fsIn = new FileStream(clearFileIn, FileMode.Open, FileAccess.Read)) { @@ -232,13 +273,21 @@ public static void Encrypt(string clearFileIn, string encryptedFileOut, byte[] k /// Encrypt a stream into a file /// public static void Encrypt(Stream clearStreamIn, string encryptedFileOut, byte[] key, byte[] iv) + { + Encrypt(clearStreamIn, encryptedFileOut, key, iv, KeySize.Default, BlockSize.Default); + } + + /// + /// Encrypt a stream into a file + /// + public static void Encrypt(Stream clearStreamIn, string encryptedFileOut, byte[] key, byte[] iv, KeySize keySize, BlockSize blockSize) { if (clearStreamIn == null) throw new ArgumentNullException(nameof(clearStreamIn)); if (string.IsNullOrEmpty(encryptedFileOut)) throw new ArgumentNullException(nameof(encryptedFileOut)); if (key == null || key.Length <= 0) throw new ArgumentNullException(nameof(key)); if (iv == null || iv.Length <= 0) throw new ArgumentNullException(nameof(iv)); - using (var alg = GetRijndaelManaged(key, iv)) + using (var alg = GetRijndaelManaged(key, iv, keySize, blockSize)) { Encrypt(clearStreamIn, encryptedFileOut, alg); } @@ -249,11 +298,20 @@ public static void Encrypt(Stream clearStreamIn, string encryptedFileOut, byte[] /// The Key and an IV are automatically generated. These will be required when Decrypting the data. /// public static void Encrypt(string clearFileIn, string encryptedFileOut, out string key, out string iv) + { + Encrypt(clearFileIn, encryptedFileOut, KeySize.Default, BlockSize.Default, out key, out iv); + } + + /// + /// Encrypt a file into another file. + /// The Key and an IV are automatically generated. These will be required when Decrypting the data. + /// + public static void Encrypt(string clearFileIn, string encryptedFileOut, KeySize keySize, BlockSize blockSize, out string key, out string iv) { if (string.IsNullOrEmpty(clearFileIn)) throw new ArgumentNullException(nameof(clearFileIn)); if (string.IsNullOrEmpty(encryptedFileOut)) throw new ArgumentNullException(nameof(encryptedFileOut)); - using (var alg = GetRijndaelManaged(null, null)) + using (var alg = GetRijndaelManaged(null, null, keySize, blockSize)) { alg.GenerateIV(); alg.GenerateKey(); @@ -273,11 +331,20 @@ public static void Encrypt(string clearFileIn, string encryptedFileOut, out stri /// The Key and an IV are automatically generated. These will be required when Decrypting the data. /// public static void Encrypt(Stream clearStreamIn, string encryptedFileOut, out string key, out string iv) + { + Encrypt(clearStreamIn, encryptedFileOut, KeySize.Default, BlockSize.Default, out key, out iv); + } + + /// + /// Encrypt a stream into a file. + /// The Key and an IV are automatically generated. These will be required when Decrypting the data. + /// + public static void Encrypt(Stream clearStreamIn, string encryptedFileOut, KeySize keySize, BlockSize blockSize, out string key, out string iv) { if (clearStreamIn == null) throw new ArgumentNullException(nameof(clearStreamIn)); if (string.IsNullOrEmpty(encryptedFileOut)) throw new ArgumentNullException(nameof(encryptedFileOut)); - using (var alg = GetRijndaelManaged(null, null)) + using (var alg = GetRijndaelManaged(null, null, keySize, blockSize)) { alg.GenerateIV(); alg.GenerateKey(); @@ -293,6 +360,14 @@ public static void Encrypt(Stream clearStreamIn, string encryptedFileOut, out st /// Decrypt a byte array into a byte array using a Key and an IV /// public static byte[] Decrypt(byte[] cipherData, byte[] key, byte[] iv) + { + return Decrypt(cipherData, key, iv, KeySize.Default, BlockSize.Default); + } + + /// + /// Decrypt a byte array into a byte array using a Key and an IV + /// + public static byte[] Decrypt(byte[] cipherData, byte[] key, byte[] iv, KeySize keySize, BlockSize blockSize) { if (cipherData == null) throw new ArgumentNullException(nameof(cipherData)); if (key == null || key.Length <= 0) throw new ArgumentNullException(nameof(key)); @@ -307,7 +382,7 @@ public static byte[] Decrypt(byte[] cipherData, byte[] key, byte[] iv) // We are going to use Rijndael because it is strong and available on all platforms. // You can use other algorithms, to do so substitute the next line with something like // TripleDES alg = TripleDES.Create(); - using (var alg = GetRijndaelManaged(key, iv)) + using (var alg = GetRijndaelManaged(key, iv, keySize, blockSize)) { // Create a CryptoStream through which we are going to be pumping our data. // CryptoStreamMode.Write means that we are going to be writing data to the stream @@ -347,13 +422,12 @@ public static void Decrypt(Stream encryptedStreamIn, Stream clearStreamOut, Rijn // Now will will initialize a buffer and will be processing the input file in chunks. // This is done to avoid reading the whole file (which can be huge) into memory. - const int bufferLen = 4096; - var buffer = new byte[bufferLen]; + var buffer = new byte[BufferLen]; int bytesRead; do { - bytesRead = encryptedStreamIn.Read(buffer, 0, bufferLen); // Read a chunk of data from the input file + bytesRead = encryptedStreamIn.Read(buffer, 0, BufferLen); // Read a chunk of data from the input file if (bytesRead > 0) cs.Write(buffer, 0, bytesRead); // Decrypt it } while (bytesRead != 0); @@ -368,6 +442,14 @@ public static void Decrypt(Stream encryptedStreamIn, Stream clearStreamOut, Rijn /// Decrypt a file into another file /// public static void Decrypt(string encryptedFileIn, string clearFileOut, byte[] key, byte[] iv) + { + Decrypt(encryptedFileIn, clearFileOut, key, iv, KeySize.Default, BlockSize.Default); + } + + /// + /// Decrypt a file into another file + /// + public static void Decrypt(string encryptedFileIn, string clearFileOut, byte[] key, byte[] iv, KeySize keySize, BlockSize blockSize) { if (string.IsNullOrEmpty(encryptedFileIn)) throw new ArgumentNullException(nameof(encryptedFileIn)); if (string.IsNullOrEmpty(clearFileOut)) throw new ArgumentNullException(nameof(clearFileOut)); @@ -379,7 +461,7 @@ public static void Decrypt(string encryptedFileIn, string clearFileOut, byte[] k { using (var fsOut = new FileStream(clearFileOut, FileMode.OpenOrCreate, FileAccess.Write)) { - using (var alg = GetRijndaelManaged(key, iv)) + using (var alg = GetRijndaelManaged(key, iv, keySize, blockSize)) { Decrypt(fsIn, fsOut, alg); } @@ -404,6 +486,14 @@ public static void Decrypt(string encryptedFileIn, string clearFileOut, string k /// Decrypt a file into another file using a Key and an IV /// public static void Decrypt(string encryptedFileIn, Stream clearStreamOut, string key, string iv) + { + Decrypt(encryptedFileIn, clearStreamOut, key, iv, KeySize.Default, BlockSize.Default); + } + + /// + /// Decrypt a file into another file using a Key and an IV + /// + public static void Decrypt(string encryptedFileIn, Stream clearStreamOut, string key, string iv, KeySize keySize, BlockSize blockSize) { if (encryptedFileIn == null) throw new ArgumentNullException(nameof(encryptedFileIn)); if (clearStreamOut == null) throw new ArgumentNullException(nameof(clearStreamOut)); @@ -412,7 +502,7 @@ public static void Decrypt(string encryptedFileIn, Stream clearStreamOut, string using (var fsIn = new FileStream(encryptedFileIn, FileMode.Open, FileAccess.Read)) { - using (var alg = GetRijndaelManaged(Convert.FromBase64String(key), Convert.FromBase64String(iv))) + using (var alg = GetRijndaelManaged(Convert.FromBase64String(key), Convert.FromBase64String(iv), keySize, blockSize)) { Decrypt(fsIn, clearStreamOut, alg); } @@ -420,7 +510,7 @@ public static void Decrypt(string encryptedFileIn, Stream clearStreamOut, string } /// - /// Converts HEX string to btye array. + /// Converts HEX string to byte array. /// Opposite of ByteArrayToHex. /// public static byte[] HexToByteArray(string hexString) diff --git a/Digest.cs b/Digest.cs index 51b0112..2f884a3 100644 --- a/Digest.cs +++ b/Digest.cs @@ -109,8 +109,7 @@ public static Digest CreateFromString(string hashedData, string sharedKey) var hashType = (HashType) int.Parse(hashedData.Substring(0, 2)); - int hashLength; - int.TryParse(hashedData.Substring(2, 3), out hashLength); + int.TryParse(hashedData.Substring(2, 3), out var hashLength); if (hashLength < 0) return null; diff --git a/Effortless.Net.Encryption.Tests.Unit/Effortless.Net.Encryption.Tests.Unit/ByteTests.cs b/Effortless.Net.Encryption.Tests.Unit/Effortless.Net.Encryption.Tests.Unit/ByteTests.cs index 535312c..b2073fb 100644 --- a/Effortless.Net.Encryption.Tests.Unit/Effortless.Net.Encryption.Tests.Unit/ByteTests.cs +++ b/Effortless.Net.Encryption.Tests.Unit/Effortless.Net.Encryption.Tests.Unit/ByteTests.cs @@ -24,7 +24,6 @@ public void TearDown() private byte[] _plainData; private string _filePlainData; private string _fileEncryptedData; - private RijndaelManaged _rijndaelManaged; [TestFixtureSetUp] public void TextFixtureSetUp() @@ -35,14 +34,6 @@ public void TextFixtureSetUp() _fileEncryptedData = Path.Combine(path, "testEncrypted.txt"); _filePlainData = Path.Combine(path, "testPlain.txt"); _plainData = GetBytes("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla auctor, justo quis rhoncus hendrerit, lacus ligula lobortis ipsum, et semper justo lorem ut tellus. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur scelerisque nisl et lacus pulvinar malesuada. In hac habitasse platea dictumst. Curabitur est metus, posuere quis pulvinar a, congue nec neque. Sed at mi vitae leo condimentum blandit. Mauris in tortor eu risus pellentesque molestie. Aliquam at leo eget erat volutpat ultricies in in purus. Quisque elit sapien, accumsan vitae sagittis ac, faucibus congue neque."); - - _rijndaelManaged = new RijndaelManaged - { - KeySize = 256, - BlockSize = 256, - Padding = PaddingMode.ISO10126, - Mode = CipherMode.CBC - }; } private static byte[] GetBytes(string str) @@ -59,33 +50,44 @@ private void DeleteOutputFile() } [Test] - public void Encrypt_32k_10_times_using_all_padding_and_cypher_modes() + public void Encrypt_4k_10_times_using_all_padding_and_cypher_modes() { - foreach (var paddingMode in (PaddingMode[])Enum.GetValues(typeof(PaddingMode))) + foreach (var paddingMode in (PaddingMode[]) Enum.GetValues(typeof(PaddingMode))) { if (paddingMode == PaddingMode.None) continue; - foreach (var cipherMode in (CipherMode[])Enum.GetValues(typeof(CipherMode))) + foreach (var cipherMode in (CipherMode[]) Enum.GetValues(typeof(CipherMode))) { - if(!Bytes.SetPaddingAndCipherModes(paddingMode, cipherMode)) + if (!Bytes.SetPaddingAndCipherModes(paddingMode, cipherMode)) continue; // invalid padding/cipher mode Console.WriteLine("Padding Mode {0} CipherMode Mode {1}", paddingMode, cipherMode); - var key = Bytes.GenerateKey(); - var iv = Bytes.GenerateIV(); - var random = new Random(); - - for (var n = 0; n < 10; n++) + foreach (var keySize in (Bytes.KeySize[]) Enum.GetValues(typeof(Bytes.KeySize))) { - var size = random.Next(32768) + 1; - var data = new byte[size]; - Bytes.GetRandomBytes(data); - - var encrypted = Bytes.Encrypt(data, key, iv); - var decrypted = Bytes.Decrypt(encrypted, key, iv); - Assert.AreEqual(data, decrypted); + if (keySize == Bytes.KeySize.Default) + continue; + foreach (var blockSize in (Bytes.BlockSize[]) Enum.GetValues(typeof(Bytes.BlockSize))) + { + if (blockSize == Bytes.BlockSize.Default) + continue; + Console.WriteLine(" KeySize {0} BlockSize {1}", keySize, blockSize); + var key = Bytes.GenerateKey(keySize, blockSize); + var iv = Bytes.GenerateIV(keySize, blockSize); + var random = new Random(); + + for (var n = 0; n < 10; n++) + { + var size = random.Next(4096) + 1; + var data = new byte[size]; + Bytes.GetRandomBytes(data); + + var encrypted = Bytes.Encrypt(data, key, iv, keySize, blockSize); + var decrypted = Bytes.Decrypt(encrypted, key, iv, keySize, blockSize); + Assert.AreEqual(data, decrypted); + } + } } } } @@ -124,28 +126,61 @@ public void Encrypt_Decrypt_file_to_file_with_generated_key_iv() [Test] public void Encrypt_Decrypt_stream_to_file_using_own_Rijndael_algorithm() { - // Encrypt file - using (var fsIn = new FileStream(_filePlainData, FileMode.Open, FileAccess.Read)) + foreach (var paddingMode in (PaddingMode[]) Enum.GetValues(typeof(PaddingMode))) { - Bytes.Encrypt(fsIn, _fileEncryptedData, _rijndaelManaged); - } + if (paddingMode == PaddingMode.None) + continue; - // Decrypt file data - using (var memoryStream = new MemoryStream()) - { - using (var fsIn = new FileStream(_fileEncryptedData, FileMode.Open, FileAccess.Read)) + foreach (var cipherMode in (CipherMode[]) Enum.GetValues(typeof(CipherMode))) { - Bytes.Decrypt(fsIn, memoryStream, _rijndaelManaged); - } - - // Verify - memoryStream.Seek(0, SeekOrigin.Begin); - Assert.AreEqual(_plainData.Length, memoryStream.Length); + if (!Bytes.SetPaddingAndCipherModes(paddingMode, cipherMode)) + continue; // invalid padding/cipher mode - foreach (var expected in _plainData) - { - var b = memoryStream.ReadByte(); - Assert.AreEqual(expected, b); + Console.WriteLine("Padding Mode {0} CipherMode Mode {1}", paddingMode, cipherMode); + foreach (var keySize in (Bytes.KeySize[]) Enum.GetValues(typeof(Bytes.KeySize))) + { + if (keySize == Bytes.KeySize.Default) + continue; + foreach (var blockSize in (Bytes.BlockSize[]) Enum.GetValues(typeof(Bytes.BlockSize))) + { + if (blockSize == Bytes.BlockSize.Default) + continue; + Console.WriteLine(" KeySize {0} BlockSize {1}", keySize, blockSize); + + var rm = new RijndaelManaged + { + KeySize = (int) keySize, + BlockSize = (int) blockSize, + Padding = paddingMode, + Mode = cipherMode + }; + + // Encrypt file + using (var fsIn = new FileStream(_filePlainData, FileMode.Open, FileAccess.Read)) + { + Bytes.Encrypt(fsIn, _fileEncryptedData, rm); + } + + // Decrypt file data + using (var memoryStream = new MemoryStream()) + { + using (var fsIn = new FileStream(_fileEncryptedData, FileMode.Open, FileAccess.Read)) + { + Bytes.Decrypt(fsIn, memoryStream, rm); + } + + // Verify + memoryStream.Seek(0, SeekOrigin.Begin); + Assert.AreEqual(_plainData.Length, memoryStream.Length); + + foreach (var expected in _plainData) + { + var b = memoryStream.ReadByte(); + Assert.AreEqual(expected, b); + } + } + } + } } } } @@ -180,15 +215,27 @@ public void Encrypt_Decrypt_stream_to_file_with_generated_key_iv() [Test] public void Generate_key_iv_encrypt_decrypt() { - var key = Bytes.GenerateKey(); - var iv = Bytes.GenerateIV(); + foreach (var keySize in (Bytes.KeySize[]) Enum.GetValues(typeof(Bytes.KeySize))) + { + if (keySize == Bytes.KeySize.Default) + continue; + foreach (var blockSize in (Bytes.BlockSize[]) Enum.GetValues(typeof(Bytes.BlockSize))) + { + if (blockSize == Bytes.BlockSize.Default) + continue; + Console.WriteLine("KeySize {0} BlockSize {1}", keySize, blockSize); - var data = new byte[1024]; - Bytes.GetRandomBytes(data); + var key = Bytes.GenerateKey(keySize, blockSize); + var iv = Bytes.GenerateIV(keySize, blockSize); - var encrypted = Bytes.Encrypt(data, key, iv); - var decrypted = Bytes.Decrypt(encrypted, key, iv); - Assert.AreEqual(data, decrypted); + var data = new byte[1024]; + Bytes.GetRandomBytes(data); + + var encrypted = Bytes.Encrypt(data, key, iv, keySize, blockSize); + var decrypted = Bytes.Decrypt(encrypted, key, iv, keySize, blockSize); + Assert.AreEqual(data, decrypted); + } + } } [Test] @@ -197,15 +244,21 @@ public void Generate_key_iv_encrypt_decrypt() [TestCase(10000)] public void Generate_key_with_password_and_salt_iv_encrypt_decrypt_128(int iterationCount) { - var key = Bytes.GenerateKey("password", "saltsaltsalt", Bytes.KeySize.Size128, iterationCount); - var iv = Bytes.GenerateIV(); + foreach (var keySize in (Bytes.KeySize[]) Enum.GetValues(typeof(Bytes.KeySize))) + { + if (keySize == Bytes.KeySize.Default) + continue; + Console.WriteLine("KeySize {0}", keySize); + var key = Bytes.GenerateKey("password", "saltsaltsalt", keySize, iterationCount); + var iv = Bytes.GenerateIV(); - var data = new byte[1024]; - Bytes.GetRandomBytes(data); + var data = new byte[1024]; + Bytes.GetRandomBytes(data); - var encrypted = Bytes.Encrypt(data, key, iv); - var decrypted = Bytes.Decrypt(encrypted, key, iv); - Assert.AreEqual(data, decrypted); + var encrypted = Bytes.Encrypt(data, key, iv); + var decrypted = Bytes.Decrypt(encrypted, key, iv); + Assert.AreEqual(data, decrypted); + } } [Test] @@ -214,15 +267,21 @@ public void Generate_key_with_password_and_salt_iv_encrypt_decrypt_128(int itera [TestCase(10000)] public void Generate_key_with_password_and_salt_iv_encrypt_decrypt_192(int iterationCount) { - var key = Bytes.GenerateKey("password", "saltsaltsalt", Bytes.KeySize.Size192, iterationCount); - var iv = Bytes.GenerateIV(); + foreach (var keySize in (Bytes.KeySize[]) Enum.GetValues(typeof(Bytes.KeySize))) + { + if (keySize == Bytes.KeySize.Default) + continue; + Console.WriteLine("KeySize {0}", keySize); + var key = Bytes.GenerateKey("password", "saltsaltsalt", keySize, iterationCount); + var iv = Bytes.GenerateIV(); - var data = new byte[1024]; - Bytes.GetRandomBytes(data); + var data = new byte[1024]; + Bytes.GetRandomBytes(data); - var encrypted = Bytes.Encrypt(data, key, iv); - var decrypted = Bytes.Decrypt(encrypted, key, iv); - Assert.AreEqual(data, decrypted); + var encrypted = Bytes.Encrypt(data, key, iv); + var decrypted = Bytes.Decrypt(encrypted, key, iv); + Assert.AreEqual(data, decrypted); + } } [Test] @@ -231,15 +290,21 @@ public void Generate_key_with_password_and_salt_iv_encrypt_decrypt_192(int itera [TestCase(10000)] public void Generate_key_with_password_and_salt_iv_encrypt_decrypt_256(int iterationCount) { - var key = Bytes.GenerateKey("password", "saltsaltsalt", Bytes.KeySize.Size256, iterationCount); - var iv = Bytes.GenerateIV(); + foreach (var keySize in (Bytes.KeySize[]) Enum.GetValues(typeof(Bytes.KeySize))) + { + if (keySize == Bytes.KeySize.Default) + continue; + Console.WriteLine("KeySize {0}", keySize); + var key = Bytes.GenerateKey("password", "saltsaltsalt", keySize, iterationCount); + var iv = Bytes.GenerateIV(); - var data = new byte[1024]; - Bytes.GetRandomBytes(data); + var data = new byte[1024]; + Bytes.GetRandomBytes(data); - var encrypted = Bytes.Encrypt(data, key, iv); - var decrypted = Bytes.Decrypt(encrypted, key, iv); - Assert.AreEqual(data, decrypted); + var encrypted = Bytes.Encrypt(data, key, iv); + var decrypted = Bytes.Decrypt(encrypted, key, iv); + Assert.AreEqual(data, decrypted); + } } [Test] diff --git a/Effortless.Net.Encryption.Tests.Unit/Effortless.Net.Encryption.Tests.Unit/HashTests.cs b/Effortless.Net.Encryption.Tests.Unit/Effortless.Net.Encryption.Tests.Unit/HashTests.cs index d3f303a..5fb69b0 100644 --- a/Effortless.Net.Encryption.Tests.Unit/Effortless.Net.Encryption.Tests.Unit/HashTests.cs +++ b/Effortless.Net.Encryption.Tests.Unit/Effortless.Net.Encryption.Tests.Unit/HashTests.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Concurrent; using System.Security.Cryptography; using System.Text; using System.Threading.Tasks; @@ -105,5 +104,22 @@ public void MultiThreaded() Assert.AreEqual(expected, base64); }); } + + [Test] + public void MultiThreaded_v2() + { + var bytes = Encoding.Default.GetBytes("Hello world"); + + var expected = Convert.ToBase64String(SHA512.Create().ComputeHash(bytes)); + Console.WriteLine("Expected"); + Console.WriteLine(expected); + Console.WriteLine(); + + Parallel.For(0, 1000, ignored => + { + var base64 = Convert.ToBase64String(SHA512.Create().ComputeHash(bytes)); + Assert.AreEqual(expected, base64); + }); + } } } \ No newline at end of file diff --git a/Effortless.Net.Encryption.Tests.Unit/Effortless.Net.Encryption.Tests.Unit/StringsTest.cs b/Effortless.Net.Encryption.Tests.Unit/Effortless.Net.Encryption.Tests.Unit/StringsTest.cs index ea4a558..8776ec2 100644 --- a/Effortless.Net.Encryption.Tests.Unit/Effortless.Net.Encryption.Tests.Unit/StringsTest.cs +++ b/Effortless.Net.Encryption.Tests.Unit/Effortless.Net.Encryption.Tests.Unit/StringsTest.cs @@ -96,9 +96,9 @@ public void Encrypt_decrypt_using_a_very_long_string() } [Test] - [TestCase(128)] - [TestCase(192)] - [TestCase(256)] + [TestCase(Bytes.KeySize.Size128)] + [TestCase(Bytes.KeySize.Size192)] + [TestCase(Bytes.KeySize.Size256)] public void Encrypt_decrypt_using_different_key_sizes(Bytes.KeySize keySize) { const string password = "Hello world";