From 7baae99cd86ade99986b6f081a93a16043d9cc50 Mon Sep 17 00:00:00 2001 From: Fu Diwei Date: Fri, 28 May 2021 19:09:36 +0800 Subject: [PATCH] =?UTF-8?q?feat(wxapi):=20=E5=AE=8C=E6=88=90=20SHA-1?= =?UTF-8?q?=E3=80=81HMAC-SHA-256=20=E5=B7=A5=E5=85=B7=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Utilities/HmacSha256Util.cs | 44 +++++++++++++++++++ .../Utilities/Sha1Util.cs | 39 ++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 src/SKIT.FlurlHttpClient.Wechat.Api/Utilities/HmacSha256Util.cs create mode 100644 src/SKIT.FlurlHttpClient.Wechat.Api/Utilities/Sha1Util.cs diff --git a/src/SKIT.FlurlHttpClient.Wechat.Api/Utilities/HmacSha256Util.cs b/src/SKIT.FlurlHttpClient.Wechat.Api/Utilities/HmacSha256Util.cs new file mode 100644 index 00000000..89064155 --- /dev/null +++ b/src/SKIT.FlurlHttpClient.Wechat.Api/Utilities/HmacSha256Util.cs @@ -0,0 +1,44 @@ +using System; +using System.Security.Cryptography; +using System.Text; + +namespace SKIT.FlurlHttpClient.Wechat.Api.Utilities +{ + /// + /// HMAC-SHA-1 算法工具类。 + /// + public static class HmacSha256Util + { + /// + /// 获取信息摘要。 + /// + /// 密钥字节数组。 + /// 信息字节数组。 + /// 信息摘要。 + public static string Hash(byte[] secretBytes, byte[] bytes) + { + if (secretBytes == null) throw new ArgumentNullException(nameof(secretBytes)); + if (bytes == null) throw new ArgumentNullException(nameof(bytes)); + + using HMAC hmac = new HMACSHA256(secretBytes); + byte[] hashBytes = hmac.ComputeHash(bytes); + return BitConverter.ToString(hashBytes).Replace("-", ""); + } + + /// + /// 获取信息摘要。 + /// + /// 密钥。 + /// 文本信息。 + /// 信息摘要。 + public static string Hash(string secret, string message) + { + if (secret == null) throw new ArgumentNullException(nameof(secret)); + if (message == null) throw new ArgumentNullException(nameof(message)); + + byte[] secretBytes = Encoding.UTF8.GetBytes(secret); + byte[] bytes = Encoding.UTF8.GetBytes(message); + return Hash(secretBytes, bytes); + } + } +} diff --git a/src/SKIT.FlurlHttpClient.Wechat.Api/Utilities/Sha1Util.cs b/src/SKIT.FlurlHttpClient.Wechat.Api/Utilities/Sha1Util.cs new file mode 100644 index 00000000..b8385dac --- /dev/null +++ b/src/SKIT.FlurlHttpClient.Wechat.Api/Utilities/Sha1Util.cs @@ -0,0 +1,39 @@ +using System; +using System.Security.Cryptography; +using System.Text; + +namespace SKIT.FlurlHttpClient.Wechat.Api.Utilities +{ + /// + /// SHA-1 算法工具类。 + /// + public static class Sha1Util + { + /// + /// 获取信息摘要。 + /// + /// 信息字节数组。 + /// 信息摘要。 + public static string Hash(byte[] bytes) + { + if (bytes == null) throw new ArgumentNullException(nameof(bytes)); + + using SHA1 sha = SHA1.Create(); + byte[] hashBytes = sha.ComputeHash(bytes); + return BitConverter.ToString(hashBytes).Replace("-", ""); + } + + /// + /// 获取信息摘要。 + /// + /// 文本信息。 + /// 信息摘要。 + public static string Hash(string message) + { + if (message == null) throw new ArgumentNullException(nameof(message)); + + byte[] bytes = Encoding.UTF8.GetBytes(message); + return Hash(bytes); + } + } +}