Skip to content

Commit

Permalink
Use Convert.FromHexString when possible instead of HexUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
sergezhigunov committed Jul 29, 2024
1 parent 3e2dac9 commit 3c3ab4c
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 56 deletions.
6 changes: 3 additions & 3 deletions tests/OpenGost.Security.Cryptography.Tests/CmacTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -47,7 +47,7 @@ public virtual void VerifyCmac(string dataHex, string keyHex, string digestHex)
key[0] = (byte)(key[0] + 1);
Assert.NotEqual<byte>(key, cmac.Key);

computedDigest = cmac.ComputeHash(dataHex.HexToByteArray());
computedDigest = cmac.ComputeHash(Convert.FromHexString(dataHex));
}

Assert.Equal(digestBytes, computedDigest);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
29 changes: 13 additions & 16 deletions tests/OpenGost.Security.Cryptography.Tests/GostECDsaManagedFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,27 @@ 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
{
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
Expand Down Expand Up @@ -427,10 +424,10 @@ public static TheoryData<ECParameters, byte[], byte[]> TestCases
{
TestDomainParameters256,
// Hash
HexUtils.HexToByteArray(
Convert.FromHexString(
"e53e042b67e6ec678e2e02b12a0352ce1fc6eee0529cc088119ad872b3c1fb2d"),
// Signature
HexUtils.HexToByteArray(
Convert.FromHexString(
// s
"01456c64ba4642a1653c235a98a60249bcd6d3f746b631df928014f6c5bf9c40" +
// r
Expand All @@ -440,11 +437,11 @@ public static TheoryData<ECParameters, byte[], byte[]> TestCases
{
TestDomainParameters512,
// Hash
HexUtils.HexToByteArray(
Convert.FromHexString(
"8c5b0772297d77c64f0c561ddbde7a405a5d7c646c97394341f4936553ee8471" +
"91c5b03570141da733c570c1f9b6091b53ab8d4d7c4a4f5c61e0c9accff35437"),
// Signature
HexUtils.HexToByteArray(
Convert.FromHexString(
// s
"1081b394696ffe8e6585e7a9362d26b6325f56778aadbc081c0bfbe933d52ff5" +
"823ce288e8c4f362526080df7f70ce406a6eeb1f56919cb92a9853bde73e5b4a" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ public abstract class HashAlgorithmTest<T>
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)
{
Expand Down
36 changes: 10 additions & 26 deletions tests/OpenGost.Security.Cryptography.Tests/HexUtils.cs
Original file line number Diff line number Diff line change
@@ -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);

Expand All @@ -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)
{
Expand All @@ -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')
Expand All @@ -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
6 changes: 3 additions & 3 deletions tests/OpenGost.Security.Cryptography.Tests/HmacTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -75,7 +75,7 @@ public virtual void VerifyHmac(string dataHex, string keyHex, string digestHex)
key[0] = (byte)(key[0] + 1);
Assert.NotEqual<byte>(key, hmac.Key);

computedDigest = hmac.ComputeHash(dataHex.HexToByteArray());
computedDigest = hmac.ComputeHash(Convert.FromHexString(dataHex));
}

Assert.Equal(digestBytes, computedDigest);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);

Expand Down

0 comments on commit 3c3ab4c

Please sign in to comment.