Skip to content

Commit

Permalink
Use new .NET APIs
Browse files Browse the repository at this point in the history
- Use `Convert.ToHexStringLower()`.
- Use `CryptographicOperations.HashData()`.
  • Loading branch information
martincostello committed Feb 14, 2024
1 parent 02353a8 commit 86ce2cd
Showing 1 changed file with 14 additions and 15 deletions.
29 changes: 14 additions & 15 deletions src/API/ApiModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,12 @@ public static IEndpointRouteBuilder MapApiEndpoints(this IEndpointRouteBuilder b
/// Returns a <see cref="string"/> containing a hexadecimal representation of the specified <see cref="ReadOnlySpan{T}"/> of bytes.
/// </summary>
/// <param name="bytes">The buffer to generate the hash string for.</param>
/// <param name="toLower">Whether to return the hash in lowercase.</param>
/// <returns>
/// A <see cref="string"/> containing the hexadecimal representation of <paramref name="bytes"/>.
/// </returns>
private static string BytesToHexString(ReadOnlySpan<byte> bytes)
=> Convert.ToHexString(bytes);
private static string BytesToHexString(ReadOnlySpan<byte> bytes, bool toLower = false)
=> toLower ? Convert.ToHexStringLower(bytes) : Convert.ToHexString(bytes);

[OpenApiExample<TimeResponse>]
[OpenApiOperation("Gets the current UTC time.", "Gets the current date and time in UTC.")]
Expand Down Expand Up @@ -170,28 +171,26 @@ private static Ok<TimeResponse> GetTime(TimeProvider timeProvider)
}

byte[] buffer = Encoding.UTF8.GetBytes(request.Plaintext ?? string.Empty);
byte[] hash = request.Algorithm.ToUpperInvariant() switch
HashAlgorithmName? hashAlgorithm = request.Algorithm.ToUpperInvariant() switch
{
#pragma warning disable CA5350
#pragma warning disable CA5351
"MD5" => MD5.HashData(buffer),
"SHA1" => SHA1.HashData(buffer),
#pragma warning restore CA5350
#pragma warning restore CA5351
"SHA256" => SHA256.HashData(buffer),
"SHA384" => SHA384.HashData(buffer),
"SHA512" => SHA512.HashData(buffer),
_ => [],
"MD5" => HashAlgorithmName.MD5,
"SHA1" => HashAlgorithmName.SHA1,
"SHA256" => HashAlgorithmName.SHA256,
"SHA384" => HashAlgorithmName.SHA384,
"SHA512" => HashAlgorithmName.SHA512,
_ => null,

Check warning on line 181 in src/API/ApiModule.cs

View check run for this annotation

Codecov / codecov/patch

src/API/ApiModule.cs#L181

Added line #L181 was not covered by tests
};

if (hash.Length == 0)
if (hashAlgorithm is not { } algorithm)
{
return Results.Extensions.InvalidRequest($"The specified hash algorithm '{request.Algorithm}' is not supported.");
}

byte[] hash = CryptographicOperations.HashData(algorithm, buffer);

var result = new HashResponse()
{
Hash = formatAsBase64 ? Convert.ToBase64String(hash) : BytesToHexString(hash).ToLowerInvariant(),
Hash = formatAsBase64 ? Convert.ToBase64String(hash) : BytesToHexString(hash, toLower: true),
};

return TypedResults.Json(result, ApplicationJsonSerializerContext.Default.HashResponse);
Expand Down

0 comments on commit 86ce2cd

Please sign in to comment.