Blame view

netcore/src/Infrastructure/NCC.Pay/NCC.Security/HMAC_SHA256.cs 846 Bytes
de2bd2f9   “wangming”   项目初始化
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
  using System;
  using System.Text;
  
  namespace NCC.Core.Pay.Security
  {
      public static class HMAC_SHA256
      {
          public static string Compute(string data, string key)
          {
              if (string.IsNullOrEmpty(data))
              {
                  throw new ArgumentNullException(nameof(data));
              }
  
              if (string.IsNullOrEmpty(key))
              {
                  throw new ArgumentNullException(nameof(key));
              }
  
              using (var hmacSha256 = new System.Security.Cryptography.HMACSHA256(Encoding.UTF8.GetBytes(key)))
              {
                  var hash = hmacSha256.ComputeHash(Encoding.UTF8.GetBytes(data));
  #if NET5_0_OR_GREATER
                  return Convert.ToHexString(hash);
  #else
                  return BitConverter.ToString(hash).Replace("-", "");
  #endif
              }
          }
      }
  }