refactor(tenpayv2): 优化加解密及哈希算法工具类

This commit is contained in:
Fu Diwei
2024-02-04 22:53:24 +08:00
committed by RHQYZ
parent 3a4e65b1c6
commit 69955a1f2b
8 changed files with 70 additions and 56 deletions

View File

@@ -1,8 +1,9 @@
using System;
using System.Text;
namespace SKIT.FlurlHttpClient.Wechat.TenpayV2
{
using SKIT.FlurlHttpClient.Primitives;
/// <summary>
/// 为 <see cref="WechatTenpayClient"/> 提供回调通知事件敏感数据解密的扩展方法。
/// </summary>
@@ -56,11 +57,11 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV2
try
{
string key = Utilities.MD5Utility.Hash(client.Credentials.MerchantSecret).ToLower();
string key = Utilities.MD5Utility.Hash(client.Credentials.MerchantSecret).Value!.ToLower();
string plainXml = Utilities.AESUtility.DecryptWithECB(
encodingKey: Convert.ToBase64String(Encoding.UTF8.GetBytes(key)),
encodingCipherText: webhookEvent.EncryptedRequestInfo!
);
encodingKey: new EncodedString(key, EncodingKinds.Literal),
encodingCipher: new EncodedString(webhookEvent.EncryptedRequestInfo!, EncodingKinds.Base64)
)!;
plainJson = Utilities.XmlHelper.ConvertToJson(plainXml);
}
catch (Exception ex)

View File

@@ -10,6 +10,8 @@ using Flurl.Http;
namespace SKIT.FlurlHttpClient.Wechat.TenpayV2
{
using SKIT.FlurlHttpClient.Primitives;
public static class WechatTenpayClientExecuteMerchantMediaExtensions
{
/// <summary>
@@ -29,7 +31,7 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV2
request.FileName = Guid.NewGuid().ToString("N").ToLower() + ".jpg";
if (request.FileHash is null)
request.FileHash = BitConverter.ToString(Utilities.MD5Utility.Hash(request.FileBytes ?? Array.Empty<byte>())).Replace("-", string.Empty).ToLower();
request.FileHash = EncodedString.ToHexString(Utilities.MD5Utility.Hash(request.FileBytes ?? Array.Empty<byte>())).Value!.ToLower();
IFlurlRequest flurlReq = client
.CreateFlurlRequest(request, HttpMethod.Post, "secapi", "mch", "uploadmedia");

View File

@@ -10,7 +10,7 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV2.Models
[System.Text.Json.Serialization.JsonConverter(typeof(Converters.ResponseClassSystemTextJsonConverter))]
public class GetTransitPartnerOrderResponse : WechatTenpaySignableResponse
{
public static new class Types
public static class Types
{
public class Coupon : GetTransitOrderResponse.Types.Coupon
{

View File

@@ -1,9 +1,10 @@
using System;
using System.Security.Cryptography;
using System.Text;
namespace SKIT.FlurlHttpClient.Wechat.TenpayV2.Utilities
{
using SKIT.FlurlHttpClient.Primitives;
/// <summary>
/// AES 算法工具类。
/// </summary>
@@ -20,33 +21,31 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV2.Utilities
if (keyBytes is null) throw new ArgumentNullException(nameof(keyBytes));
if (cipherBytes is null) throw new ArgumentNullException(nameof(cipherBytes));
using (SymmetricAlgorithm aes = Aes.Create())
{
aes.Mode = CipherMode.ECB;
aes.Padding = PaddingMode.PKCS7;
aes.Key = keyBytes;
using SymmetricAlgorithm aes = Aes.Create();
aes.Mode = CipherMode.ECB;
aes.Padding = PaddingMode.PKCS7;
aes.Key = keyBytes;
using ICryptoTransform transform = aes.CreateDecryptor();
return transform.TransformFinalBlock(cipherBytes, 0, cipherBytes.Length);
}
using ICryptoTransform transform = aes.CreateDecryptor();
return transform.TransformFinalBlock(cipherBytes, 0, cipherBytes.Length);
}
/// <summary>
/// 基于 ECB 模式解密数据。
/// </summary>
/// <param name="encodingKey">经 Base64 编码后的 AES 密钥。</param>
/// <param name="encodingCipherText">经 Base64 编码后的待解密数据。</param>
/// <returns>解密后的文本数据。</returns>
public static string DecryptWithECB(string encodingKey, string encodingCipherText)
/// <param name="encodingKey">经过编码后的(通常为 Base64AES 密钥。</param>
/// <param name="encodingCipher">经过编码后的(通常为 Base64待解密数据。</param>
/// <returns>解密后的数据。</returns>
public static EncodedString DecryptWithECB(EncodedString encodingKey, EncodedString encodingCipher)
{
if (encodingKey is null) throw new ArgumentNullException(nameof(encodingKey));
if (encodingCipherText is null) throw new ArgumentNullException(nameof(encodingCipherText));
if (encodingKey.Value is null) throw new ArgumentNullException(nameof(encodingKey));
if (encodingCipher.Value is null) throw new ArgumentNullException(nameof(encodingCipher));
byte[] plainBytes = DecryptWithECB(
keyBytes: Convert.FromBase64String(encodingKey),
cipherBytes: Convert.FromBase64String(encodingCipherText)
keyBytes: EncodedString.FromString(encodingKey, fallbackEncodingKind: EncodingKinds.Base64),
cipherBytes: EncodedString.FromString(encodingCipher, fallbackEncodingKind: EncodingKinds.Base64)
);
return Encoding.UTF8.GetString(plainBytes);
return EncodedString.ToLiteralString(plainBytes);
}
}
}

View File

@@ -1,44 +1,49 @@
using System;
using System.Security.Cryptography;
using System.Text;
namespace SKIT.FlurlHttpClient.Wechat.TenpayV2.Utilities
{
using SKIT.FlurlHttpClient.Primitives;
/// <summary>
/// HMAC 算法工具类。
/// </summary>
public static class HMACUtility
{
/// <summary>
/// 获取 HMAC-SHA-256 消息认证码
/// 计算 HMAC-SHA-256 哈希值
/// </summary>
/// <param name="secretBytes">密钥字节数组。</param>
/// <param name="msgBytes">信息字节数组。</param>
/// <returns>消息认证码字节数组。</returns>
public static byte[] HashWithSHA256(byte[] secretBytes, byte[] msgBytes)
/// <param name="keyBytes">密钥字节数组。</param>
/// <param name="msgBytes">要计算哈希值的信息字节数组。</param>
/// <returns>哈希值字节数组。</returns>
public static byte[] HashWithSHA256(byte[] keyBytes, byte[] msgBytes)
{
if (secretBytes is null) throw new ArgumentNullException(nameof(secretBytes));
if (keyBytes is null) throw new ArgumentNullException(nameof(keyBytes));
if (msgBytes is null) throw new ArgumentNullException(nameof(msgBytes));
using HMAC hmac = new HMACSHA256(secretBytes);
#if NET5_0_OR_GREATER
return HMACSHA256.HashData(keyBytes, msgBytes);
#else
using HMAC hmac = new HMACSHA256(keyBytes);
return hmac.ComputeHash(msgBytes);
#endif
}
/// <summary>
/// 获取 HMAC-SHA-256 消息认证码
/// 计算 HMAC-SHA-256 哈希值
/// </summary>
/// <param name="secret">密钥。</param>
/// <param name="message">文本信息。</param>
/// <returns>消息认证码。</returns>
public static string HashWithSHA256(string secret, string message)
/// <param name="key">密钥。</param>
/// <param name="message">要计算哈希值的信息。</param>
/// <returns>经过十六进制编码的哈希值。</returns>
public static EncodedString HashWithSHA256(string key, string message)
{
if (secret is null) throw new ArgumentNullException(nameof(secret));
if (key is null) throw new ArgumentNullException(nameof(key));
if (message is null) throw new ArgumentNullException(nameof(message));
byte[] secretBytes = Encoding.UTF8.GetBytes(secret);
byte[] msgBytes = Encoding.UTF8.GetBytes(message);
byte[] hashBytes = HashWithSHA256(secretBytes, msgBytes);
return BitConverter.ToString(hashBytes).Replace("-", string.Empty);
byte[] keyBytes = EncodedString.FromLiteralString(key);
byte[] msgBytes = EncodedString.FromLiteralString(message);
byte[] hashBytes = HashWithSHA256(keyBytes, msgBytes);
return EncodedString.ToHexString(hashBytes);
}
}
}

View File

@@ -1,39 +1,44 @@
using System;
using System.Security.Cryptography;
using System.Text;
namespace SKIT.FlurlHttpClient.Wechat.TenpayV2.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("-", string.Empty);
return EncodedString.ToHexString(hashBytes);
}
}
}

View File

@@ -46,12 +46,12 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV2.Utilities
{
case Constants.SignTypes.MD5:
{
return MD5Utility.Hash(queryString).ToUpper();
return MD5Utility.Hash(queryString).Value!.ToUpper();
}
case Constants.SignTypes.HMAC_SHA256:
{
return HMACUtility.HashWithSHA256(secretValue, queryString).ToUpper();
return HMACUtility.HashWithSHA256(secretValue, queryString).Value!.ToUpper();
}
default:

View File

@@ -4,6 +4,8 @@ using Xunit;
namespace SKIT.FlurlHttpClient.Wechat.TenpayV2.UnitTests
{
using SKIT.FlurlHttpClient.Primitives;
public class TestCase_ToolsAESUtilityTests
{
[Fact(DisplayName = "测试用例AES-ECB 解密")]
@@ -12,7 +14,7 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV2.UnitTests
string key = "01234567890abcdefedcba9876543210";
string cipherText = "l/aL5GyVfl/tw3Lww6GvCQ==";
string actualPlain = Utilities.AESUtility.DecryptWithECB(encodingKey: Convert.ToBase64String(Encoding.UTF8.GetBytes(key)), encodingCipherText: cipherText);
string actualPlain = Utilities.AESUtility.DecryptWithECB(encodingKey: (EncodedString)Convert.ToBase64String(Encoding.UTF8.GetBytes(key)), encodingCipher: (EncodedString)cipherText)!;
string expectedPlain = "SKIT is great!";
Assert.Equal(expectedPlain, actualPlain);