mirror of
https://gitee.com/fudiwei/DotNetCore.SKIT.FlurlHttpClient.Wechat.git
synced 2025-07-16 07:59:44 +08:00
refactor(wxads): 优化哈希算法工具类
This commit is contained in:
parent
6630c357ca
commit
6511d9205c
@ -9,6 +9,8 @@ using Flurl.Http;
|
||||
|
||||
namespace SKIT.FlurlHttpClient.Wechat.Ads
|
||||
{
|
||||
using SKIT.FlurlHttpClient.Primitives;
|
||||
|
||||
public static class WechatAdsClientExecuteImagesExtensions
|
||||
{
|
||||
/// <summary>
|
||||
@ -24,19 +26,13 @@ namespace SKIT.FlurlHttpClient.Wechat.Ads
|
||||
if (request is null) throw new ArgumentNullException(nameof(request));
|
||||
|
||||
if (request.FileName is null)
|
||||
{
|
||||
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)
|
||||
{
|
||||
request.FileHash = BitConverter.ToString(Utilities.MD5Utility.Hash(request.FileBytes ?? Array.Empty<byte>())).Replace("-", string.Empty);
|
||||
}
|
||||
request.FileHash = EncodedString.ToHexString(Utilities.MD5Utility.Hash(request.FileBytes ?? Array.Empty<byte>())).Value!;
|
||||
|
||||
if (request.FileContentType is null)
|
||||
request.FileContentType = Utilities.FileNameToContentTypeMapper.GetContentTypeForImage(request.FileName!) ?? "image/png";
|
||||
|
||||
string boundary = "--BOUNDARY--" + DateTimeOffset.Now.Ticks.ToString("x");
|
||||
using var fileContent = new ByteArrayContent(request.FileBytes ?? Array.Empty<byte>());
|
||||
|
@ -25,7 +25,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Ads.Interceptors
|
||||
|
||||
string timestamp = DateTimeOffset.Now.ToLocalTime().ToUnixTimeSeconds().ToString();
|
||||
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}"));
|
||||
|
||||
context.FlurlCall.Request.SetQueryParam("agency_token", token);
|
||||
|
@ -1,39 +1,44 @@
|
||||
using System;
|
||||
using System;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
|
||||
namespace SKIT.FlurlHttpClient.Wechat.Ads.Utilities
|
||||
{
|
||||
using SKIT.FlurlHttpClient.Primitives;
|
||||
|
||||
/// <summary>
|
||||
/// MD5 算法工具类。
|
||||
/// </summary>
|
||||
public static class MD5Utility
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取 MD5 信息摘要。
|
||||
/// 计算 MD5 哈希值。
|
||||
/// </summary>
|
||||
/// <param name="bytes">信息字节数组。</param>
|
||||
/// <returns>信息摘要字节数组。</returns>
|
||||
/// <param name="bytes">要计算哈希值的信息字节数组。</param>
|
||||
/// <returns>哈希值字节数组。</returns>
|
||||
public static byte[] Hash(byte[] 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();
|
||||
return md5.ComputeHash(bytes);
|
||||
#endif
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取 MD5 信息摘要。
|
||||
/// 计算 MD5 哈希值。
|
||||
/// </summary>
|
||||
/// <param name="message">文本信息。</param>
|
||||
/// <returns>信息摘要。</returns>
|
||||
public static string Hash(string message)
|
||||
/// <param name="message">要计算哈希值的信息。</param>
|
||||
/// <returns>经过十六进制编码的哈希值。</returns>
|
||||
public static EncodedString Hash(string 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);
|
||||
return BitConverter.ToString(hashBytes).Replace("-", "");
|
||||
return EncodedString.ToHexString(hashBytes);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -4,12 +4,12 @@ namespace SKIT.FlurlHttpClient.Wechat.Ads.UnitTests
|
||||
{
|
||||
public class TestCase_ToolsHashUtilityTests
|
||||
{
|
||||
[Fact(DisplayName = "测试用例:MD5 信息摘要")]
|
||||
[Fact(DisplayName = "测试用例:MD5 哈希值")]
|
||||
public void TestMD5Hash()
|
||||
{
|
||||
string rawData = "spidbff89d5138160943040012345678901234567890uFolxxiZbrZ/PRbyen5uK5D1kgIB2yHyDsfDGxxgeG";
|
||||
|
||||
string actualHash = Utilities.MD5Utility.Hash(rawData);
|
||||
string actualHash = Utilities.MD5Utility.Hash(rawData)!;
|
||||
string expectedHash = "32c03e8fcdb08e653e42805e302f70ed";
|
||||
|
||||
Assert.Equal(expectedHash, actualHash, ignoreCase: true);
|
||||
|
Loading…
Reference in New Issue
Block a user