Skip to content

Commit

Permalink
code refact
Browse files Browse the repository at this point in the history
  • Loading branch information
EdiWang committed May 23, 2024
1 parent dc9501d commit 276b0f6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 21 deletions.
20 changes: 0 additions & 20 deletions src/Moonglade.Utils/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,26 +112,6 @@ public static string GetDNSPrefetchUrl(string cdnEndpoint)
return $"{uri.Scheme}://{uri.Host}/";
}

public static string GetMd5Hash(string input)
{
// Convert the input string to a byte array and compute the hash.
var data = MD5.Create().ComputeHash(Encoding.UTF8.GetBytes(input));

// Create a new Stringbuilder to collect the bytes
// and create a string.
var sBuilder = new StringBuilder();

// Loop through each byte of the hashed data
// and format each one as a hexadecimal string.
foreach (var t in data)
{
sBuilder.Append(t.ToString("x2"));
}

// Return the hexadecimal string.
return sBuilder.ToString();
}

// https://docs.microsoft.com/en-us/aspnet/core/security/data-protection/consumer-apis/password-hashing?view=aspnetcore-6.0
// This is not secure, but better than nothing.
public static string HashPassword(string clearPassword, string saltBase64)
Expand Down
23 changes: 22 additions & 1 deletion src/Moonglade.Web/TagHelpers/GravatarImgHelper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Razor.TagHelpers;
using System.Security.Cryptography;
using System.Text.Encodings.Web;

namespace Moonglade.Web.TagHelpers;
Expand All @@ -19,7 +20,7 @@ public class GravatarImgHelper : TagHelper
public override void Process(TagHelperContext context, TagHelperOutput output)
{
var email = string.IsNullOrEmpty(Email) ? string.Empty : Email.Trim().ToLower();
var emailHash = Helper.GetMd5Hash(email);
var emailHash = GetMd5Hash(email);

var src = string.Format("{0}://{1}.gravatar.com/avatar/{2}?s={3}{4}{5}{6}",
PreferHttps ? "https" : "http",
Expand All @@ -36,4 +37,24 @@ public override void Process(TagHelperContext context, TagHelperOutput output)
output.Attributes.SetAttribute("src", src);
output.Attributes.SetAttribute("alt", "Gravatar image");
}

private static string GetMd5Hash(string input)
{
// Convert the input string to a byte array and compute the hash.
var data = MD5.Create().ComputeHash(Encoding.UTF8.GetBytes(input));

// Create a new Stringbuilder to collect the bytes
// and create a string.
var sBuilder = new StringBuilder();

// Loop through each byte of the hashed data
// and format each one as a hexadecimal string.
foreach (var t in data)
{
sBuilder.Append(t.ToString("x2"));
}

// Return the hexadecimal string.
return sBuilder.ToString();
}
}

0 comments on commit 276b0f6

Please sign in to comment.