diff --git a/Src/Tests/Bitcoin/Cryptography/Hashing/HmacSha256Tests.cs b/Src/Tests/Bitcoin/Cryptography/Hashing/HmacSha256Tests.cs index 359fa61..ca0b20f 100644 --- a/Src/Tests/Bitcoin/Cryptography/Hashing/HmacSha256Tests.cs +++ b/Src/Tests/Bitcoin/Cryptography/Hashing/HmacSha256Tests.cs @@ -7,7 +7,6 @@ using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; -using Xunit; namespace Tests.Bitcoin.Cryptography.Hashing { @@ -28,7 +27,7 @@ public void ConstructorTest(byte[] key, bool isKeyHashed) byte[] expectedKey = sysHmac.Key; // Set key in constructor: - using (HmacSha256 hmac = new HmacSha256(key)) + using (HmacSha256 hmac = new(key)) { Assert.Equal(expectedKey, hmac.Key); if (!isKeyHashed) @@ -44,7 +43,7 @@ public void ConstructorTest(byte[] key, bool isKeyHashed) } // Set key using the property: - using (HmacSha256 hmac = new HmacSha256()) + using (HmacSha256 hmac = new()) { Assert.Null(hmac.Key); hmac.Key = key; @@ -66,7 +65,7 @@ public void Constructor_ExceptionTest() { Assert.Throws(() => new HmacSha256(null)); - HmacSha256 hmac = new HmacSha256(); + HmacSha256 hmac = new(); Assert.Throws(() => hmac.Key = null); } @@ -74,7 +73,7 @@ public void Constructor_ExceptionTest() [Fact] public void ComputeHash_ExceptionTest() { - HmacSha256 hmac = new HmacSha256(); + HmacSha256 hmac = new(); Exception ex = Assert.Throws(() => hmac.ComputeHash(null)); Assert.Contains("Data can not be null", ex.Message); @@ -93,7 +92,7 @@ public void ComputeHash_ExceptionTest() [Fact] public void ComputeHash_WithKey_ExceptionTest() { - HmacSha256 hmac = new HmacSha256(); + HmacSha256 hmac = new(); Exception ex = Assert.Throws(() => hmac.ComputeHash(null, new byte[1])); Assert.Contains("Data can not be null", ex.Message); @@ -115,7 +114,7 @@ public void ComputeHash_EmptyTest(byte[] key, byte[] data) using var sysHmac = new System.Security.Cryptography.HMACSHA256(key); byte[] expected = sysHmac.ComputeHash(data); - using HmacSha256 hmac = new HmacSha256(key); + using HmacSha256 hmac = new(key); byte[] actual = hmac.ComputeHash(data); Assert.Equal(expected, actual); } @@ -129,7 +128,7 @@ public void ComputeHash_Empty_WithKey_Test(byte[] key, byte[] data) using var sysHmac = new System.Security.Cryptography.HMACSHA256(key); byte[] expected = sysHmac.ComputeHash(data); - using HmacSha256 hmac = new HmacSha256(); + using HmacSha256 hmac = new(); byte[] actual = hmac.ComputeHash(data, key); Assert.Equal(expected, actual); } @@ -138,7 +137,7 @@ public void ComputeHash_Empty_WithKey_Test(byte[] key, byte[] data) public static TheoryData GetHmacSha_Rfc_Cases() { // Test cases are taken from https://tools.ietf.org/html/rfc4231 - TheoryData result = new TheoryData(); + TheoryData result = new(); foreach (var item in Helper.ReadResource("HmacShaRfcTestData")) { byte[] msgBytes = Helper.HexToBytes(item["Message"].ToString()); @@ -155,7 +154,7 @@ public static TheoryData GetHmacSha_Rfc_Cases() [MemberData(nameof(HashTestCaseHelper.GetHmacSha_Rfc_Cases), parameters: 256, MemberType = typeof(HashTestCaseHelper))] public void ComputeHash_rfc_Test(byte[] msg, byte[] key, byte[] expected) { - using HmacSha256 hmac = new HmacSha256(); + using HmacSha256 hmac = new(); byte[] actual = hmac.ComputeHash(msg, key); Assert.Equal(expected, actual); } @@ -164,7 +163,7 @@ public void ComputeHash_rfc_Test(byte[] msg, byte[] key, byte[] expected) [MemberData(nameof(HashTestCaseHelper.GetHmacSha_Rfc_Cases), parameters: 256, MemberType = typeof(HashTestCaseHelper))] public void ComputeHash_rfc_CtorKey_Test(byte[] msg, byte[] key, byte[] expected) { - using HmacSha256 hmac = new HmacSha256(key); + using HmacSha256 hmac = new(key); byte[] actual = hmac.ComputeHash(msg); Assert.Equal(expected, actual); } @@ -174,7 +173,7 @@ public void ComputeHash_rfc_CtorKey_Test(byte[] msg, byte[] key, byte[] expected [MemberData(nameof(HashTestCaseHelper.GetHmacSha_Nist_Cases), parameters: 256, MemberType = typeof(HashTestCaseHelper))] public void ComputeHash_NIST_Test(byte[] msg, byte[] key, byte[] expected, int len, bool truncate) { - using HmacSha256 hmac = new HmacSha256(); + using HmacSha256 hmac = new(); byte[] actual = hmac.ComputeHash(msg, key); if (truncate) { @@ -189,7 +188,7 @@ public void ComputeHash_NIST_Test(byte[] msg, byte[] key, byte[] expected, int l [MemberData(nameof(HashTestCaseHelper.GetHmacSha_Nist_Cases), parameters: 256, MemberType = typeof(HashTestCaseHelper))] public void ComputeHash_NIST_CtorKey_Test(byte[] msg, byte[] key, byte[] expected, int len, bool truncate) { - using HmacSha256 hmac = new HmacSha256(key); + using HmacSha256 hmac = new(key); byte[] actual = hmac.ComputeHash(msg); if (truncate) { @@ -201,7 +200,7 @@ public void ComputeHash_NIST_CtorKey_Test(byte[] msg, byte[] key, byte[] expecte } - public static TheoryData GetReuseCases() + public static TheoryData GetReuseCases() { byte[] key1 = Helper.HexToBytes("27d94d0e34b0066e29f23c9a6597cfe77a1df7e27f2740914c9a44e49c6a7b12"); byte[] data1_1 = Helper.HexToBytes("24344356060cde62834b9a1e6143f89fac4485eb8993e8a811226f74e670fbf4"); @@ -229,7 +228,7 @@ public static TheoryData GetReuseCases() public void ComputeHash_CtorKey_ReuseTest(byte[] key1, byte[] data1_1, byte[] exp1_1, byte[] data1_2, byte[] exp1_2, byte[] key2, byte[] data2_1, byte[] exp2_1, byte[] data2_2, byte[] exp2_2) { - using HmacSha256 hmac = new HmacSha256(key1); + using HmacSha256 hmac = new(key1); byte[] actual1_1 = hmac.ComputeHash(data1_1); Assert.Equal(exp1_1, actual1_1); @@ -250,7 +249,7 @@ public void ComputeHash_CtorKey_ReuseTest(byte[] key1, byte[] data1_1, byte[] ex public void ComputeHash_Reuse_Test(byte[] key1, byte[] data1_1, byte[] exp1_1, byte[] data1_2, byte[] exp1_2, byte[] key2, byte[] data2_1, byte[] exp2_1, byte[] data2_2, byte[] exp2_2) { - using HmacSha256 hmac = new HmacSha256(); + using HmacSha256 hmac = new(); byte[] actual1_1 = hmac.ComputeHash(data1_1, key1); Assert.Equal(exp1_1, actual1_1); diff --git a/Src/Tests/Bitcoin/Cryptography/Hashing/HmacSha512Tests.cs b/Src/Tests/Bitcoin/Cryptography/Hashing/HmacSha512Tests.cs index 20453b3..509b59f 100644 --- a/Src/Tests/Bitcoin/Cryptography/Hashing/HmacSha512Tests.cs +++ b/Src/Tests/Bitcoin/Cryptography/Hashing/HmacSha512Tests.cs @@ -4,10 +4,8 @@ // file LICENCE or http://www.opensource.org/licenses/mit-license.php. using Autarkysoft.Bitcoin.Cryptography.Hashing; -using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; -using Xunit; namespace Tests.Bitcoin.Cryptography.Hashing { @@ -28,7 +26,7 @@ public void ConstructorTest(byte[] key, bool isKeyHashed) byte[] expectedKey = sysHmac.Key; // Set key in constructor: - using (HmacSha512 hmac = new HmacSha512(key)) + using (HmacSha512 hmac = new(key)) { Assert.Equal(expectedKey, hmac.Key); if (!isKeyHashed) @@ -44,7 +42,7 @@ public void ConstructorTest(byte[] key, bool isKeyHashed) } // Set key using the property: - using (HmacSha512 hmac = new HmacSha512()) + using (HmacSha512 hmac = new()) { Assert.Null(hmac.Key); hmac.Key = key; @@ -66,7 +64,7 @@ public void Constructor_ExceptionTest() { Assert.Throws(() => new HmacSha512(null)); - HmacSha512 hmac = new HmacSha512(); + HmacSha512 hmac = new(); Assert.Throws(() => hmac.Key = null); } @@ -74,7 +72,7 @@ public void Constructor_ExceptionTest() [Fact] public void ComputeHash_ExceptionTest() { - HmacSha512 hmac = new HmacSha512(); + HmacSha512 hmac = new(); Exception ex = Assert.Throws(() => hmac.ComputeHash(null)); Assert.Contains("Data can not be null", ex.Message); @@ -93,7 +91,7 @@ public void ComputeHash_ExceptionTest() [Fact] public void ComputeHash_WithKey_ExceptionTest() { - HmacSha512 hmac = new HmacSha512(); + HmacSha512 hmac = new(); Exception ex = Assert.Throws(() => hmac.ComputeHash(null, new byte[1])); Assert.Contains("Data can not be null", ex.Message); @@ -115,7 +113,7 @@ public void ComputeHash_EmptyTest(byte[] key, byte[] data) using var sysHmac = new System.Security.Cryptography.HMACSHA512(key); byte[] expected = sysHmac.ComputeHash(data); - using HmacSha512 hmac = new HmacSha512(key); + using HmacSha512 hmac = new(key); byte[] actual = hmac.ComputeHash(data); Assert.Equal(expected, actual); } @@ -129,7 +127,7 @@ public void ComputeHash_Empty_WithKey_Test(byte[] key, byte[] data) using var sysHmac = new System.Security.Cryptography.HMACSHA512(key); byte[] expected = sysHmac.ComputeHash(data); - using HmacSha512 hmac = new HmacSha512(); + using HmacSha512 hmac = new(); byte[] actual = hmac.ComputeHash(data, key); Assert.Equal(expected, actual); } @@ -139,7 +137,7 @@ public void ComputeHash_Empty_WithKey_Test(byte[] key, byte[] data) [MemberData(nameof(HashTestCaseHelper.GetHmacSha_Rfc_Cases), parameters: 512, MemberType = typeof(HashTestCaseHelper))] public void ComputeHash_rfc_Test(byte[] msg, byte[] key, byte[] expected) { - using HmacSha512 hmac = new HmacSha512(); + using HmacSha512 hmac = new(); byte[] actual = hmac.ComputeHash(msg, key); Assert.Equal(expected, actual); } @@ -148,7 +146,7 @@ public void ComputeHash_rfc_Test(byte[] msg, byte[] key, byte[] expected) [MemberData(nameof(HashTestCaseHelper.GetHmacSha_Rfc_Cases), parameters: 512, MemberType = typeof(HashTestCaseHelper))] public void ComputeHash_rfc_CtorKey_Test(byte[] msg, byte[] key, byte[] expected) { - using HmacSha512 hmac = new HmacSha512(key); + using HmacSha512 hmac = new(key); byte[] actual = hmac.ComputeHash(msg); Assert.Equal(expected, actual); } @@ -158,7 +156,7 @@ public void ComputeHash_rfc_CtorKey_Test(byte[] msg, byte[] key, byte[] expected [MemberData(nameof(HashTestCaseHelper.GetHmacSha_Nist_Cases), parameters: 512, MemberType = typeof(HashTestCaseHelper))] public void ComputeHash_NIST_Test(byte[] msg, byte[] key, byte[] expected, int len, bool truncate) { - using HmacSha512 hmac = new HmacSha512(); + using HmacSha512 hmac = new(); byte[] actual = hmac.ComputeHash(msg, key); if (truncate) { @@ -173,7 +171,7 @@ public void ComputeHash_NIST_Test(byte[] msg, byte[] key, byte[] expected, int l [MemberData(nameof(HashTestCaseHelper.GetHmacSha_Nist_Cases), parameters: 512, MemberType = typeof(HashTestCaseHelper))] public void ComputeHash_NIST_CtorKey_Test(byte[] msg, byte[] key, byte[] expected, int len, bool truncate) { - using HmacSha512 hmac = new HmacSha512(key); + using HmacSha512 hmac = new(key); byte[] actual = hmac.ComputeHash(msg); if (truncate) { @@ -185,7 +183,7 @@ public void ComputeHash_NIST_CtorKey_Test(byte[] msg, byte[] key, byte[] expecte } - public static TheoryData GetReuseCases() + public static TheoryData GetReuseCases() { byte[] key1 = Helper.HexToBytes("27d94d0e34b0066e29f23c9a6597cfe77a1df7e27f2740914c9a44e49c6a7b12"); byte[] data1_1 = Helper.HexToBytes("24344356060cde62834b9a1e6143f89fac4485eb8993e8a811226f74e670fbf4"); @@ -213,7 +211,7 @@ public static TheoryData GetReuseCases() public void ComputeHash_CtorKey_ReuseTest(byte[] key1, byte[] data1_1, byte[] exp1_1, byte[] data1_2, byte[] exp1_2, byte[] key2, byte[] data2_1, byte[] exp2_1, byte[] data2_2, byte[] exp2_2) { - using HmacSha512 hmac = new HmacSha512(key1); + using HmacSha512 hmac = new(key1); byte[] actual1_1 = hmac.ComputeHash(data1_1); Assert.Equal(exp1_1, actual1_1); @@ -234,7 +232,7 @@ public void ComputeHash_CtorKey_ReuseTest(byte[] key1, byte[] data1_1, byte[] ex public void ComputeHash_Reuse_Test(byte[] key1, byte[] data1_1, byte[] exp1_1, byte[] data1_2, byte[] exp1_2, byte[] key2, byte[] data2_1, byte[] exp2_1, byte[] data2_2, byte[] exp2_2) { - using HmacSha512 hmac = new HmacSha512(); + using HmacSha512 hmac = new(); byte[] actual1_1 = hmac.ComputeHash(data1_1, key1); Assert.Equal(exp1_1, actual1_1); diff --git a/Src/Tests/Bitcoin/Cryptography/Hashing/Ripemd160Tests.cs b/Src/Tests/Bitcoin/Cryptography/Hashing/Ripemd160Tests.cs index 125bf19..861dc0c 100644 --- a/Src/Tests/Bitcoin/Cryptography/Hashing/Ripemd160Tests.cs +++ b/Src/Tests/Bitcoin/Cryptography/Hashing/Ripemd160Tests.cs @@ -6,8 +6,8 @@ using Autarkysoft.Bitcoin.Cryptography.Hashing; using Newtonsoft.Json.Linq; using System; +using System.Collections.Generic; using System.Text; -using Xunit; namespace Tests.Bitcoin.Cryptography.Hashing { @@ -17,7 +17,7 @@ public class Ripemd160Tests [MemberData(nameof(HashTestCaseHelper.GetCommonHashCases), "RIPEMD160", MemberType = typeof(HashTestCaseHelper))] public void ComputeHashTest(byte[] message, byte[] expectedHash) { - using Ripemd160 rip = new Ripemd160(); + using Ripemd160 rip = new(); byte[] actualHash = rip.ComputeHash(message); Assert.Equal(expectedHash, actualHash); } @@ -32,9 +32,8 @@ private static byte[] GetBytes(int len) return result; } - public static TheoryData GetProgressiveCase() + public static IEnumerable GetProgressiveCase() { - TheoryData result = new TheoryData(); int len = 1; // Hash values were computed using .Net framework 4.7.2 System.Security.Cryptography.RIPEMD160Managed foreach (var item in Helper.ReadResource("Ripemd160ProgressiveTestData")) @@ -42,15 +41,14 @@ public static TheoryData GetProgressiveCase() byte[] msgBytes = GetBytes(len++); byte[] hashBytes = Helper.HexToBytes(item.ToString()); - result.Add(msgBytes, hashBytes); + yield return new object[] { msgBytes, hashBytes }; } - return result; } [Theory] [MemberData(nameof(GetProgressiveCase))] public void ComputeHash_ProgressiveTest(byte[] message, byte[] expectedHash) { - using Ripemd160 rip = new Ripemd160(); + using Ripemd160 rip = new(); byte[] actualHash = rip.ComputeHash(message); Assert.Equal(expectedHash, actualHash); } @@ -58,7 +56,7 @@ public void ComputeHash_ProgressiveTest(byte[] message, byte[] expectedHash) [Fact] public void ComputeHash_AMillionATest() { - using Ripemd160 rip = new Ripemd160(); + using Ripemd160 rip = new(); byte[] actualHash = rip.ComputeHash(HashTestCaseHelper.GetAMillionA()); byte[] expectedHash = Helper.HexToBytes("52783243c1697bdbe16d37f97f68f08325dc1528"); @@ -74,7 +72,7 @@ public void ComputeHash_ReuseTest() byte[] exp1 = Helper.HexToBytes("37f332f68db77bd9d7edd4969571ad671cf9dd3b"); byte[] exp2 = Helper.HexToBytes("132072df690933835eb8b6ad0b77e7b6f14acad7"); - using Ripemd160 rip = new Ripemd160(); + using Ripemd160 rip = new(); byte[] act1 = rip.ComputeHash(msg1); byte[] act2 = rip.ComputeHash(msg2); @@ -85,7 +83,7 @@ public void ComputeHash_ReuseTest() [Fact] public void ComputeHash_WithIndexTest() { - using Ripemd160 rip = new Ripemd160(); + using Ripemd160 rip = new(); byte[] data = Helper.HexToBytes("123fab54686520717569636b2062726f776e20666f78206a756d7073206f76657220746865206c617a7920646f67f3a25c92"); byte[] actualHash = rip.ComputeHash(data, 3, 43); byte[] expectedHash = Helper.HexToBytes("37f332f68db77bd9d7edd4969571ad671cf9dd3b"); @@ -97,8 +95,8 @@ public void ComputeHash_WithIndexTest() [Fact] public void ComputeHash_ExceptionsTest() { - byte[] goodBa = { 1, 2, 3 }; - Ripemd160 rip = new Ripemd160(); + byte[] goodBa = [1, 2, 3]; + Ripemd160 rip = new(); Assert.Throws(() => rip.ComputeHash(null)); Assert.Throws(() => rip.ComputeHash(null, 0, 1));