refactor(wxads): 优化哈希算法工具类

This commit is contained in:
Fu Diwei 2024-02-04 22:51:32 +08:00 committed by RHQYZ
parent 6630c357ca
commit 6511d9205c
4 changed files with 25 additions and 24 deletions

View File

@ -9,6 +9,8 @@ using Flurl.Http;
namespace SKIT.FlurlHttpClient.Wechat.Ads namespace SKIT.FlurlHttpClient.Wechat.Ads
{ {
using SKIT.FlurlHttpClient.Primitives;
public static class WechatAdsClientExecuteImagesExtensions public static class WechatAdsClientExecuteImagesExtensions
{ {
/// <summary> /// <summary>
@ -24,19 +26,13 @@ namespace SKIT.FlurlHttpClient.Wechat.Ads
if (request is null) throw new ArgumentNullException(nameof(request)); if (request is null) throw new ArgumentNullException(nameof(request));
if (request.FileName is null) if (request.FileName is null)
{
request.FileName = Guid.NewGuid().ToString("N").ToLower(); request.FileName = Guid.NewGuid().ToString("N").ToLower();
}
if (request.FileContentType is null)
{
request.FileContentType = Utilities.FileNameToContentTypeMapper.GetContentTypeForImage(request.FileName!) ?? "image/png";
}
if (request.FileHash is null) if (request.FileHash is null)
{ request.FileHash = EncodedString.ToHexString(Utilities.MD5Utility.Hash(request.FileBytes ?? Array.Empty<byte>())).Value!;
request.FileHash = BitConverter.ToString(Utilities.MD5Utility.Hash(request.FileBytes ?? Array.Empty<byte>())).Replace("-", string.Empty);
} if (request.FileContentType is null)
request.FileContentType = Utilities.FileNameToContentTypeMapper.GetContentTypeForImage(request.FileName!) ?? "image/png";
string boundary = "--BOUNDARY--" + DateTimeOffset.Now.Ticks.ToString("x"); string boundary = "--BOUNDARY--" + DateTimeOffset.Now.Ticks.ToString("x");
using var fileContent = new ByteArrayContent(request.FileBytes ?? Array.Empty<byte>()); using var fileContent = new ByteArrayContent(request.FileBytes ?? Array.Empty<byte>());

View File

@ -25,7 +25,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Ads.Interceptors
string timestamp = DateTimeOffset.Now.ToLocalTime().ToUnixTimeSeconds().ToString(); string timestamp = DateTimeOffset.Now.ToLocalTime().ToUnixTimeSeconds().ToString();
string nonce = Guid.NewGuid().ToString("N"); string nonce = Guid.NewGuid().ToString("N");
string sign = Utilities.MD5Utility.Hash($"{_agencyId}{timestamp}{nonce}{_agencyApiKey}").ToLower(); string sign = Utilities.MD5Utility.Hash($"{_agencyId}{timestamp}{nonce}{_agencyApiKey}").Value!.ToLower();
string token = Convert.ToBase64String(Encoding.UTF8.GetBytes($"{_agencyId},{timestamp},{nonce},{sign}")); string token = Convert.ToBase64String(Encoding.UTF8.GetBytes($"{_agencyId},{timestamp},{nonce},{sign}"));
context.FlurlCall.Request.SetQueryParam("agency_token", token); context.FlurlCall.Request.SetQueryParam("agency_token", token);

View File

@ -1,39 +1,44 @@
using System; using System;
using System.Security.Cryptography; using System.Security.Cryptography;
using System.Text;
namespace SKIT.FlurlHttpClient.Wechat.Ads.Utilities namespace SKIT.FlurlHttpClient.Wechat.Ads.Utilities
{ {
using SKIT.FlurlHttpClient.Primitives;
/// <summary> /// <summary>
/// MD5 算法工具类。 /// MD5 算法工具类。
/// </summary> /// </summary>
public static class MD5Utility public static class MD5Utility
{ {
/// <summary> /// <summary>
/// 获取 MD5 信息摘要 /// 计算 MD5 哈希值
/// </summary> /// </summary>
/// <param name="bytes">信息字节数组。</param> /// <param name="bytes">要计算哈希值的信息字节数组。</param>
/// <returns>信息摘要字节数组。</returns> /// <returns>哈希值字节数组。</returns>
public static byte[] Hash(byte[] bytes) public static byte[] Hash(byte[] bytes)
{ {
if (bytes is null) throw new ArgumentNullException(nameof(bytes)); if (bytes is null) throw new ArgumentNullException(nameof(bytes));
#if NET5_0_OR_GREATER
return MD5.HashData(bytes);
#else
using MD5 md5 = MD5.Create(); using MD5 md5 = MD5.Create();
return md5.ComputeHash(bytes); return md5.ComputeHash(bytes);
#endif
} }
/// <summary> /// <summary>
/// 获取 MD5 信息摘要 /// 计算 MD5 哈希值
/// </summary> /// </summary>
/// <param name="message">文本信息。</param> /// <param name="message">要计算哈希值的信息。</param>
/// <returns>信息摘要。</returns> /// <returns>经过十六进制编码的哈希值。</returns>
public static string Hash(string message) public static EncodedString Hash(string message)
{ {
if (message is null) throw new ArgumentNullException(nameof(message)); if (message is null) throw new ArgumentNullException(nameof(message));
byte[] msgBytes = Encoding.UTF8.GetBytes(message); byte[] msgBytes = EncodedString.FromLiteralString(message);
byte[] hashBytes = Hash(msgBytes); byte[] hashBytes = Hash(msgBytes);
return BitConverter.ToString(hashBytes).Replace("-", ""); return EncodedString.ToHexString(hashBytes);
} }
} }
} }

View File

@ -4,12 +4,12 @@ namespace SKIT.FlurlHttpClient.Wechat.Ads.UnitTests
{ {
public class TestCase_ToolsHashUtilityTests public class TestCase_ToolsHashUtilityTests
{ {
[Fact(DisplayName = "测试用例MD5 信息摘要")] [Fact(DisplayName = "测试用例MD5 哈希值")]
public void TestMD5Hash() public void TestMD5Hash()
{ {
string rawData = "spidbff89d5138160943040012345678901234567890uFolxxiZbrZ/PRbyen5uK5D1kgIB2yHyDsfDGxxgeG"; string rawData = "spidbff89d5138160943040012345678901234567890uFolxxiZbrZ/PRbyen5uK5D1kgIB2yHyDsfDGxxgeG";
string actualHash = Utilities.MD5Utility.Hash(rawData); string actualHash = Utilities.MD5Utility.Hash(rawData)!;
string expectedHash = "32c03e8fcdb08e653e42805e302f70ed"; string expectedHash = "32c03e8fcdb08e653e42805e302f70ed";
Assert.Equal(expectedHash, actualHash, ignoreCase: true); Assert.Equal(expectedHash, actualHash, ignoreCase: true);