diff --git a/tests/OpenGost.Security.Cryptography.Tests/CmacTest.cs b/tests/OpenGost.Security.Cryptography.Tests/CmacTest.cs index 68c16d4..c75bf99 100644 --- a/tests/OpenGost.Security.Cryptography.Tests/CmacTest.cs +++ b/tests/OpenGost.Security.Cryptography.Tests/CmacTest.cs @@ -29,14 +29,14 @@ public void Constructor_WithoutParameters_InitializesInstance() public virtual void VerifyCmac(string dataHex, string keyHex, string digestHex) { - var digestBytes = digestHex.HexToByteArray(); + var digestBytes = Convert.FromHexString(digestHex); byte[] computedDigest; using (var cmac = new T()) { Assert.True(cmac.HashSize > 0); - var key = keyHex.HexToByteArray(); + var key = Convert.FromHexString(keyHex); cmac.Key = key; // make sure the getter returns different objects each time @@ -47,7 +47,7 @@ public virtual void VerifyCmac(string dataHex, string keyHex, string digestHex) key[0] = (byte)(key[0] + 1); Assert.NotEqual(key, cmac.Key); - computedDigest = cmac.ComputeHash(dataHex.HexToByteArray()); + computedDigest = cmac.ComputeHash(Convert.FromHexString(dataHex)); } Assert.Equal(digestBytes, computedDigest); diff --git a/tests/OpenGost.Security.Cryptography.Tests/GostECDsaCertificateExtensionsFacts.cs b/tests/OpenGost.Security.Cryptography.Tests/GostECDsaCertificateExtensionsFacts.cs index ca69051..087166c 100644 --- a/tests/OpenGost.Security.Cryptography.Tests/GostECDsaCertificateExtensionsFacts.cs +++ b/tests/OpenGost.Security.Cryptography.Tests/GostECDsaCertificateExtensionsFacts.cs @@ -35,10 +35,10 @@ public void GetGostECDsaPublicKey_ReturnsPublicKey( using var certificate = GetCertificate(certificateName); var point = new ECPoint { - X = HexUtils.HexToByteArray(publicKeyXHexData), - Y = HexUtils.HexToByteArray(publicKeyYHexData), + X = Convert.FromHexString(publicKeyXHexData), + Y = Convert.FromHexString(publicKeyYHexData), }; - _ = HexUtils.HexToByteArray(privateKeyHexData); + _ = Convert.FromHexString(privateKeyHexData); using var publicKey = certificate.GetGostECDsaPublicKey(); diff --git a/tests/OpenGost.Security.Cryptography.Tests/GostECDsaManagedFacts.cs b/tests/OpenGost.Security.Cryptography.Tests/GostECDsaManagedFacts.cs index bd43d2f..df36f1d 100644 --- a/tests/OpenGost.Security.Cryptography.Tests/GostECDsaManagedFacts.cs +++ b/tests/OpenGost.Security.Cryptography.Tests/GostECDsaManagedFacts.cs @@ -13,10 +13,10 @@ public class GostECDsaManagedFacts Curve = ECCurve.CreateFromValue("1.2.643.7.1.2.1.1.0"), Q = new ECPoint { - X = "0bd86fe5d8db89668f789b4e1dba8585c5508b45ec5b59d8906ddb70e2492b7f".HexToByteArray(), - Y = "da77ff871a10fbdf2766d293c5d164afbb3c7b973a41c885d11d70d689b4f126".HexToByteArray(), + X = Convert.FromHexString("0bd86fe5d8db89668f789b4e1dba8585c5508b45ec5b59d8906ddb70e2492b7f"), + Y = Convert.FromHexString("da77ff871a10fbdf2766d293c5d164afbb3c7b973a41c885d11d70d689b4f126"), }, - D = "283bec9198ce191dee7e39491f96601bc1729ad39d35ed10beb99b78de9a927a".HexToByteArray(), + D = Convert.FromHexString("283bec9198ce191dee7e39491f96601bc1729ad39d35ed10beb99b78de9a927a"), }; private static ECParameters TestDomainParameters512 { get; } = new ECParameters @@ -24,19 +24,16 @@ public class GostECDsaManagedFacts Curve = ECCurve.CreateFromValue("1.2.643.7.1.2.1.2.0"), Q = new ECPoint { - X = ( + X = Convert.FromHexString( "e1ef30d52c6133ddd99d1d5c41455cf7df4d8b4c925bbc69af1433d15658515a" + - "dd2146850c325c5b81c133be655aa8c4d440e7b98a8d59487b0c7696bcc55d11") - .HexToByteArray(), - Y = ( + "dd2146850c325c5b81c133be655aa8c4d440e7b98a8d59487b0c7696bcc55d11"), + Y = Convert.FromHexString( "ecbe7736a9ec357ff2fd39931f4e114cb8cda359270ac7f0e7ff43d9419419ea" + - "61fd2ab77f5d9f63523d3b50a04f63e2a0cf51b7c13adc21560f0bd40cc9c737") - .HexToByteArray(), + "61fd2ab77f5d9f63523d3b50a04f63e2a0cf51b7c13adc21560f0bd40cc9c737"), }, - D = ( + D = Convert.FromHexString( "d48da11f826729c6dfaa18fd7b6b63a214277e82d2da223356a000223b12e872" + - "20108b508e50e70e70694651e8a09130c9d75677d43609a41b24aead8a04a60b") - .HexToByteArray(), + "20108b508e50e70e70694651e8a09130c9d75677d43609a41b24aead8a04a60b"), }; #endregion @@ -427,10 +424,10 @@ public static TheoryData TestCases { TestDomainParameters256, // Hash - HexUtils.HexToByteArray( + Convert.FromHexString( "e53e042b67e6ec678e2e02b12a0352ce1fc6eee0529cc088119ad872b3c1fb2d"), // Signature - HexUtils.HexToByteArray( + Convert.FromHexString( // s "01456c64ba4642a1653c235a98a60249bcd6d3f746b631df928014f6c5bf9c40" + // r @@ -440,11 +437,11 @@ public static TheoryData TestCases { TestDomainParameters512, // Hash - HexUtils.HexToByteArray( + Convert.FromHexString( "8c5b0772297d77c64f0c561ddbde7a405a5d7c646c97394341f4936553ee8471" + "91c5b03570141da733c570c1f9b6091b53ab8d4d7c4a4f5c61e0c9accff35437"), // Signature - HexUtils.HexToByteArray( + Convert.FromHexString( // s "1081b394696ffe8e6585e7a9362d26b6325f56778aadbc081c0bfbe933d52ff5" + "823ce288e8c4f362526080df7f70ce406a6eeb1f56919cb92a9853bde73e5b4a" + diff --git a/tests/OpenGost.Security.Cryptography.Tests/HashAlgorithmTest.cs b/tests/OpenGost.Security.Cryptography.Tests/HashAlgorithmTest.cs index e7813fc..54a3c31 100644 --- a/tests/OpenGost.Security.Cryptography.Tests/HashAlgorithmTest.cs +++ b/tests/OpenGost.Security.Cryptography.Tests/HashAlgorithmTest.cs @@ -4,7 +4,7 @@ public abstract class HashAlgorithmTest where T : HashAlgorithm, new() { protected void Verify(string input, string expected) - => Verify(input.HexToByteArray(), expected.HexToByteArray()); + => Verify(Convert.FromHexString(input), Convert.FromHexString(expected)); protected void Verify(byte[] input, byte[] expected) { diff --git a/tests/OpenGost.Security.Cryptography.Tests/HexUtils.cs b/tests/OpenGost.Security.Cryptography.Tests/HexUtils.cs index 6a41f5d..b23ac6d 100644 --- a/tests/OpenGost.Security.Cryptography.Tests/HexUtils.cs +++ b/tests/OpenGost.Security.Cryptography.Tests/HexUtils.cs @@ -1,18 +1,16 @@ -using System.Diagnostics.CodeAnalysis; -using System.Text; +#if !NET6_0_OR_GREATER +using System.Diagnostics.CodeAnalysis; namespace OpenGost.Security.Cryptography.Tests; [ExcludeFromCodeCoverage] -internal static class HexUtils +internal static class Convert { - private const string HexAlphabet = "0123456789abcdef"; - - public static byte[] HexToByteArray(this string hexString) + public static byte[] FromHexString(this string s) { - if (hexString is null) - throw new ArgumentNullException(nameof(hexString)); - var hexStringLength = hexString.Length; + if (s is null) + throw new ArgumentNullException(nameof(s)); + var hexStringLength = s.Length; if (hexStringLength % 2 != 0) throw CreateTextIncorrectFormatException(null); @@ -22,8 +20,8 @@ public static byte[] HexToByteArray(this string hexString) try { retval[j] = (byte) - ((hexString[i].GetHexadecimalIndex() << 4) ^ - hexString[i + 1].GetHexadecimalIndex()); + ((s[i].GetHexadecimalIndex() << 4) ^ + s[i + 1].GetHexadecimalIndex()); } catch (FormatException formatException) { @@ -33,21 +31,6 @@ public static byte[] HexToByteArray(this string hexString) return retval; } - public static string ToHexString(this byte[] bytes) - { - if (bytes is null) - throw new ArgumentNullException(nameof(bytes)); - - var builder = new StringBuilder(bytes.Length * 2); - - foreach (var b in bytes) - { - builder.Append(HexAlphabet[b >> 4]); - builder.Append(HexAlphabet[b & 0x0F]); - } - return builder.ToString(); - } - private static byte GetHexadecimalIndex(this char character) { if (character >= '0' && character <= '9') @@ -62,3 +45,4 @@ private static byte GetHexadecimalIndex(this char character) private static FormatException CreateTextIncorrectFormatException(Exception? innerException) => new("Input text has incorrect format.", innerException); } +#endif diff --git a/tests/OpenGost.Security.Cryptography.Tests/HmacTest.cs b/tests/OpenGost.Security.Cryptography.Tests/HmacTest.cs index 4b244f2..1f761c3 100644 --- a/tests/OpenGost.Security.Cryptography.Tests/HmacTest.cs +++ b/tests/OpenGost.Security.Cryptography.Tests/HmacTest.cs @@ -57,14 +57,14 @@ public void Key_WhenSetsAfterStart_ThrowsCryptographicException() public virtual void VerifyHmac(string dataHex, string keyHex, string digestHex) { - var digestBytes = digestHex.HexToByteArray(); + var digestBytes = Convert.FromHexString(digestHex); byte[] computedDigest; using (var hmac = new T()) { Assert.True(hmac.HashSize > 0); - var key = keyHex.HexToByteArray(); + var key = Convert.FromHexString(keyHex); hmac.Key = key; // make sure the getter returns different objects each time @@ -75,7 +75,7 @@ public virtual void VerifyHmac(string dataHex, string keyHex, string digestHex) key[0] = (byte)(key[0] + 1); Assert.NotEqual(key, hmac.Key); - computedDigest = hmac.ComputeHash(dataHex.HexToByteArray()); + computedDigest = hmac.ComputeHash(Convert.FromHexString(dataHex)); } Assert.Equal(digestBytes, computedDigest); diff --git a/tests/OpenGost.Security.Cryptography.Tests/SymmetricAlgorithmTest.cs b/tests/OpenGost.Security.Cryptography.Tests/SymmetricAlgorithmTest.cs index 5461e61..a27030b 100644 --- a/tests/OpenGost.Security.Cryptography.Tests/SymmetricAlgorithmTest.cs +++ b/tests/OpenGost.Security.Cryptography.Tests/SymmetricAlgorithmTest.cs @@ -62,9 +62,9 @@ public virtual void Verify( string ivHex) { byte[] - plainTextBytes = plainTextHex.HexToByteArray(), - keyBytes = keyHex.HexToByteArray(), - ivBytes = ivHex.HexToByteArray(); + plainTextBytes = Convert.FromHexString(plainTextHex), + keyBytes = Convert.FromHexString(keyHex), + ivBytes = Convert.FromHexString(ivHex); using var algorithm = new T { Mode = mode, Padding = padding, Key = keyBytes, IV = ivBytes }; byte[] encryptedBytes; @@ -78,7 +78,7 @@ public virtual void Verify( Assert.NotEqual(plainTextBytes, encryptedBytes); - var cipherTextBytes = cipherTextHex.HexToByteArray(); + var cipherTextBytes = Convert.FromHexString(cipherTextHex); Assert.Equal(cipherTextBytes, encryptedBytes);