Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use one-shot hash APIs in a couple of places #2159

Merged
merged 1 commit into from
Jul 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1213,11 +1213,18 @@ private static string CalculateBase64UrlEncodedHash(string data)

private static string CalculateBase64UrlEncodedHash(byte[] bytes)
{
byte[] hashedBytes;

#if NET6_0_OR_GREATER
hashedBytes = SHA256.HashData(bytes);
#else
using (var hash = SHA256.Create())
{
var hashedBytes = hash.ComputeHash(bytes);
return Base64UrlEncoder.Encode(hashedBytes);
hashedBytes = hash.ComputeHash(bytes);
}
#endif

return Base64UrlEncoder.Encode(hashedBytes);
}

/// <summary>
Expand Down
8 changes: 7 additions & 1 deletion src/Microsoft.IdentityModel.Tokens/Utility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -251,10 +251,16 @@ internal static void Zero(byte[] byteArray)

internal static byte[] GenerateSha256Hash(string input)
{
byte[] bytes = Encoding.UTF8.GetBytes(input);

#if NET6_0_OR_GREATER
return SHA256.HashData(bytes);
#else
using (var hash = SHA256.Create())
{
return hash.ComputeHash(Encoding.UTF8.GetBytes(input));
return hash.ComputeHash(bytes);
}
#endif
}
}
}