Skip to content

Commit

Permalink
Use one-shot hash APIs in a couple of places (#2159)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephentoub authored Jul 26, 2023
1 parent d1fd8fa commit 057ceab
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
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
}
}
}

0 comments on commit 057ceab

Please sign in to comment.