Skip to content

Commit 707c62c

Browse files
Use HashData() and ToHexString() methods
Use new HashData() methods for SHA256 and MD5. Use Convert.ToHexString().
1 parent e5032c4 commit 707c62c

File tree

2 files changed

+9
-14
lines changed

2 files changed

+9
-14
lines changed

src/AspNet.Security.OAuth.Deezer/DeezerAuthenticationHandler.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,7 @@ protected override string BuildChallengeUrl([NotNull] AuthenticationProperties p
128128
// Store this for use during the code redemption.
129129
properties.Items.Add(OAuthConstants.CodeVerifierKey, codeVerifier);
130130

131-
using var sha256 = HashAlgorithm.Create("SHA256");
132-
byte[] challengeBytes = sha256!.ComputeHash(Encoding.UTF8.GetBytes(codeVerifier));
131+
byte[] challengeBytes = SHA256.HashData(Encoding.UTF8.GetBytes(codeVerifier));
133132
parameters[OAuthConstants.CodeChallengeKey] = WebEncoders.Base64UrlEncode(challengeBytes);
134133
parameters[OAuthConstants.CodeChallengeMethodKey] = OAuthConstants.CodeChallengeMethodS256;
135134
}

src/AspNet.Security.OAuth.Odnoklassniki/OdnoklassnikiAuthenticationHandler.cs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
* for more information concerning the license and the contributors participating to this project.
55
*/
66

7+
using System;
78
using System.Collections.Generic;
8-
using System.Globalization;
99
using System.Net.Http;
1010
using System.Net.Http.Headers;
1111
using System.Security.Claims;
@@ -39,9 +39,8 @@ protected override async Task<AuthenticationTicket> CreateTicketAsync(
3939
[NotNull] AuthenticationProperties properties,
4040
[NotNull] OAuthTokenResponse tokens)
4141
{
42-
using var algorithm = HashAlgorithm.Create("MD5");
43-
string accessSecret = GetHash(algorithm!, tokens.AccessToken + Options.ClientSecret);
44-
string sign = GetHash(algorithm!, $"application_key={Options.PublicSecret}format=jsonmethod=users.getCurrentUser{accessSecret}");
42+
string accessSecret = GetMD5Hash(tokens.AccessToken + Options.ClientSecret);
43+
string sign = GetMD5Hash($"application_key={Options.PublicSecret}format=jsonmethod=users.getCurrentUser{accessSecret}");
4544

4645
string address = QueryHelpers.AddQueryString(Options.UserInformationEndpoint, new Dictionary<string, string?>
4746
{
@@ -77,16 +76,13 @@ protected override async Task<AuthenticationTicket> CreateTicketAsync(
7776
return new AuthenticationTicket(context.Principal!, context.Properties, Scheme.Name);
7877
}
7978

80-
private static string GetHash(HashAlgorithm algorithm, string input)
79+
private static string GetMD5Hash(string input)
8180
{
82-
var builder = new StringBuilder();
81+
#pragma warning disable CA5351
82+
byte[] hash = MD5.HashData(Encoding.UTF8.GetBytes(input));
83+
#pragma warning restore CA5351
8384

84-
foreach (byte b in algorithm.ComputeHash(Encoding.UTF8.GetBytes(input)))
85-
{
86-
builder.Append(b.ToString("x2", CultureInfo.InvariantCulture));
87-
}
88-
89-
return builder.ToString();
85+
return Convert.ToHexString(hash).ToLowerInvariant();
9086
}
9187
}
9288
}

0 commit comments

Comments
 (0)