Skip to content

Commit b229b0d

Browse files
committed
Add missing translations to HashAlgorithmNames
In PR dotnet#84132, the addition of SHA3 variants to the HashAlgorithmNames was incomplete, leaving out the explicit handling of the SHA3 variants from the two helper methods: - `ToUpper()` - Would just return the name **unchanged** (which is likely wrong as it would allow _sha3-256_ through instead of uppercasing to _SHA3-256_. That's probably wrong. - `ToAlgorithmName()` - Would return the `ToString()` on the supplied `hashAlgorithm` instance, which would be the classname of the argument `hashAlgorithm` (e.g. `"System.Security.Cryptography.SHA3_256"`). That would not match the expectation of `"SHA3-256"` based on the other instances. I am not sure how important these are, or if they should be wrapped in a `#if NET8_0_OR_GREATER` wrapper (only some of the new SHA3 stuff was wrapped in that PR)
1 parent 125b216 commit b229b0d

3 files changed

Lines changed: 114 additions & 1 deletion

File tree

src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/HashAlgorithmNames.cs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ internal static partial class HashAlgorithmNames
1313
public const string SHA256 = "SHA256";
1414
public const string SHA384 = "SHA384";
1515
public const string SHA512 = "SHA512";
16-
1716
public const string SHA3_256 = "SHA3-256";
1817
public const string SHA3_384 = "SHA3-384";
1918
public const string SHA3_512 = "SHA3-512";
@@ -31,6 +30,12 @@ internal static partial class HashAlgorithmNames
3130
return HashAlgorithmNames.SHA384;
3231
if (hashAlgorithm is SHA512)
3332
return HashAlgorithmNames.SHA512;
33+
if (hashAlgorithm is SHA3_256)
34+
return HashAlgorithmNames.SHA3_256;
35+
if (hashAlgorithm is SHA3_384)
36+
return HashAlgorithmNames.SHA3_384;
37+
if (hashAlgorithm is SHA3_512)
38+
return HashAlgorithmNames.SHA3_512;
3439
if (hashAlgorithm is MD5)
3540
return HashAlgorithmNames.MD5;
3641

@@ -63,6 +68,21 @@ public static string ToUpper(string hashAlgorithmName)
6368
return SHA1;
6469
}
6570

71+
if (hashAlgorithmName.Equals(SHA3_256, StringComparison.OrdinalIgnoreCase))
72+
{
73+
return SHA3_256;
74+
}
75+
76+
if (hashAlgorithmName.Equals(SHA3_384, StringComparison.OrdinalIgnoreCase))
77+
{
78+
return SHA3_384;
79+
}
80+
81+
if (hashAlgorithmName.Equals(SHA3_512, StringComparison.OrdinalIgnoreCase))
82+
{
83+
return SHA3_512;
84+
}
85+
6686
if (hashAlgorithmName.Equals(MD5, StringComparison.OrdinalIgnoreCase))
6787
{
6888
return MD5;
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System.Collections.Generic;
5+
using Xunit;
6+
7+
namespace System.Security.Cryptography.Tests
8+
{
9+
public static class HashAlgorithmNamesTests
10+
{
11+
[Theory]
12+
[MemberData(nameof(ValidInputs))]
13+
public static void ToUpper_UnchangedValidInput(string hashAlgorithmName)
14+
{
15+
string actual = HashAlgorithmNames.ToUpper(hashAlgorithmName);
16+
Assert.Equal(hashAlgorithmName, actual);
17+
}
18+
19+
[Theory]
20+
[MemberData(nameof(ValidInputs))]
21+
public static void ToUpper_FixesLoweredValidInput(string hashAlgorithmName)
22+
{
23+
string actual = HashAlgorithmNames.ToUpper(hashAlgorithmName.ToLowerInvariant());
24+
Assert.Equal(hashAlgorithmName, actual);
25+
}
26+
27+
[Fact]
28+
public static void ToUpper_UnchangedFromInvalidInput()
29+
{
30+
string unsupported = UnsupportedHashAlgorithm.ID.ToLowerInvariant();
31+
string actual = HashAlgorithmNames.ToUpper(unsupported);
32+
Assert.Equal(unsupported, actual);
33+
}
34+
35+
[Theory]
36+
[MemberData(nameof(ValidInputs))]
37+
public static void ToAlgorithmName_ValidInput(string hashAlgorithmName)
38+
{
39+
bool hashSupported = HashProviderDispenser.HashSupported(hashAlgorithmName);
40+
41+
if (hashSupported)
42+
{
43+
using (HashAlgorithm hashAlgorithm = CreateHashAlgorithm(hashAlgorithmName);
44+
string actual = HashAlgorithmNames.ToAlgorithmName(hashAlgorithm);
45+
Assert.Equal(hashAlgorithmName, actual);
46+
}
47+
}
48+
49+
[Fact]
50+
public static void ToAlgorithmName_ToStringUnknownInput()
51+
{
52+
using (HashAlgorithm hashAlgorithm = CreateHashAlgorithm(UnsupportedHashAlgorithm.ID);
53+
string actual = HashAlgorithmNames.ToAlgorithmName(hashAlgorithm);
54+
Assert.Equal(UnsupportedHashAlgorithm.Name, actual);
55+
}
56+
57+
private static HashAlgorithm CreateHashAlgorithm(string hashAlgorithmName)
58+
{
59+
return hashAlgorithmName switch
60+
HashAlgorithmNames.MD5 => MD5.Create(),
61+
HashAlgorithmNames.SHA1 => SHA1.Create(),
62+
HashAlgorithmNames.SHA256 => SHA256.Create(),
63+
HashAlgorithmNames.SHA384 => SHA384.Create(),
64+
HashAlgorithmNames.SHA512 => SHA512.Create(),
65+
HashAlgorithmNames.SHA3_256 => SHA3_256.Create(),
66+
HashAlgorithmNames.SHA3_384 => SHA3_384.Create(),
67+
HashAlgorithmNames.SHA3_512 => SHA3_512.Create(),
68+
UnsupportedHashAlgorithm.Name => new UnsupportedHashAlgorithm();
69+
}
70+
71+
public static IEnumerable<object[]> ValidInputs
72+
{
73+
get
74+
{
75+
yield return new object[] { HashAlgorithmNames.MD5 };
76+
yield return new object[] { HashAlgorithmNames.SHA1 };
77+
yield return new object[] { HashAlgorithmNames.SHA256 };
78+
yield return new object[] { HashAlgorithmNames.SHA384 };
79+
yield return new object[] { HashAlgorithmNames.SHA512 };
80+
yield return new object[] { HashAlgorithmNames.SHA3_256 };
81+
yield return new object[] { HashAlgorithmNames.SHA3_384 };
82+
yield return new object[] { HashAlgorithmNames.SHA3_512 };
83+
}
84+
}
85+
86+
private class UnsupportedHashAlgorithm : HashAlgorithm
87+
{
88+
public static const string Name = typeof(UnsupportedHashAlgorithm).ToString();
89+
public static const string ID = "UNKNOWN";
90+
}
91+
}
92+
}

src/libraries/System.Security.Cryptography/tests/System.Security.Cryptography.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@
251251
<Compile Include="ECDsaTests.cs" />
252252
<Compile Include="ECPemExportTests.cs" />
253253
<Compile Include="FixedTimeEqualsTests.cs" />
254+
<Compile Include="HashAlgorithmNamesTests.cs" />
254255
<Compile Include="HashAlgorithmNameTests.cs" />
255256
<Compile Include="HashAlgorithmTest.cs" />
256257
<Compile Include="HashAlgorithmTestDriver.cs" />

0 commit comments

Comments
 (0)