Skip to content

Commit 729ba94

Browse files
Use new .NET APIs
- Use `Convert.ToHexStringLower()`. - Use `CryptographicOperations.HashData()`.
1 parent 30931d5 commit 729ba94

File tree

1 file changed

+16
-19
lines changed

1 file changed

+16
-19
lines changed

src/Website/Services/ToolsService.cs

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -110,30 +110,26 @@ public IResult GenerateHash(HashRequest request)
110110
}
111111

112112
byte[] buffer = Encoding.UTF8.GetBytes(request.Plaintext ?? string.Empty);
113-
byte[] hash = request.Algorithm.ToUpperInvariant() switch
114-
{
115-
#pragma warning disable CA5350
116-
#pragma warning disable CA5351
117-
"MD5" => MD5.HashData(buffer),
118-
"SHA1" => SHA1.HashData(buffer),
119-
#pragma warning restore CA5350
120-
#pragma warning restore CA5351
121-
"SHA256" => SHA256.HashData(buffer),
122-
"SHA384" => SHA384.HashData(buffer),
123-
"SHA512" => SHA512.HashData(buffer),
124-
_ => [],
113+
HashAlgorithmName? hashAlgorithm = request.Algorithm.ToUpperInvariant() switch
114+
{
115+
"MD5" => HashAlgorithmName.MD5,
116+
"SHA1" => HashAlgorithmName.SHA1,
117+
"SHA256" => HashAlgorithmName.SHA256,
118+
"SHA384" => HashAlgorithmName.SHA384,
119+
"SHA512" => HashAlgorithmName.SHA512,
120+
_ => null,
125121
};
126122

127-
if (hash.Length == 0)
123+
if (hashAlgorithm is not { } algorithm)
128124
{
129125
return BadRequest($"The specified hash algorithm '{request.Algorithm}' is not supported.");
130126
}
131127

128+
byte[] hash = CryptographicOperations.HashData(algorithm, buffer);
129+
132130
var result = new HashResponse()
133131
{
134-
#pragma warning disable CA1308
135-
Hash = formatAsBase64 ? Convert.ToBase64String(hash) : BytesToHexString(hash).ToLowerInvariant(),
136-
#pragma warning restore CA1308
132+
Hash = formatAsBase64 ? Convert.ToBase64String(hash) : BytesToHexString(hash, toLower: true),
137133
};
138134

139135
return Results.Json(result, ApplicationJsonSerializerContext.Default.HashResponse);
@@ -175,14 +171,15 @@ public IResult GenerateMachineKey(string? decryptionAlgorithm, string? validatio
175171
}
176172

177173
/// <summary>
178-
/// Returns a <see cref="string"/> containing a hexadecimal representation of the specified <see cref="Array"/> of bytes.
174+
/// Returns a <see cref="string"/> containing a hexadecimal representation of the specified <see cref="ReadOnlySpan{T}"/> of bytes.
179175
/// </summary>
180176
/// <param name="bytes">The buffer to generate the hash string for.</param>
177+
/// <param name="toLower">Whether to return the hash in lowercase.</param>
181178
/// <returns>
182179
/// A <see cref="string"/> containing the hexadecimal representation of <paramref name="bytes"/>.
183180
/// </returns>
184-
private static string BytesToHexString(ReadOnlySpan<byte> bytes)
185-
=> Convert.ToHexString(bytes);
181+
private static string BytesToHexString(ReadOnlySpan<byte> bytes, bool toLower = false)
182+
=> toLower ? Convert.ToHexStringLower(bytes) : Convert.ToHexString(bytes);
186183

187184
/// <summary>
188185
/// Returns a result that represents a bad API request.

0 commit comments

Comments
 (0)