Bez popisu
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SHAHelper.cs 1.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. using System;
  2. using System.Security.Cryptography;
  3. using System.Text;
  4. namespace LrGetToken
  5. {
  6. /// <summary>
  7. /// SHA帮助类
  8. /// </summary>
  9. public class SHAHelper
  10. {
  11. /// <summary>
  12. /// 实现HMAC SHA256签名算法
  13. /// </summary>
  14. /// <param name="secret">加密秘钥</param>
  15. /// <param name="signContent">加密内容</param>
  16. /// <returns></returns>
  17. public static string HmacSHA256(string secret, string signContent)
  18. {
  19. string signRet = string.Empty;
  20. using (HMACSHA256 mac = new HMACSHA256(Encoding.UTF8.GetBytes(secret)))
  21. {
  22. byte[] hash = mac.ComputeHash(Encoding.UTF8.GetBytes(signContent));
  23. signRet = ToHexString(hash);
  24. }
  25. var hexBytes = Encoding.UTF8.GetBytes(signRet);
  26. return Convert.ToBase64String(hexBytes);
  27. }
  28. /// <summary>
  29. /// 转16进制
  30. /// </summary>
  31. /// <param name="bytes"></param>
  32. /// <returns></returns>
  33. protected static string ToHexString(byte[] bytes)
  34. {
  35. string hexString = string.Empty;
  36. if (bytes != null)
  37. {
  38. StringBuilder strB = new StringBuilder();
  39. foreach (byte b in bytes)
  40. {
  41. strB.AppendFormat("{0:x2}", b);
  42. }
  43. hexString = strB.ToString();
  44. }
  45. return hexString;
  46. }
  47. }
  48. }