Encoder.cs 945 B

1234567891011121314151617181920212223242526272829303132
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Security.Cryptography;
  4. using System.Text;
  5. namespace Bowin.Common.Encoder.HmacSHA256
  6. {
  7. public class HMACSHA256Encoder
  8. {
  9. public static string Encode(string key, string text)
  10. {
  11. string hash;
  12. Byte[] code = System.Text.Encoding.UTF8.GetBytes(key);
  13. using (HMACSHA256 hmac = new HMACSHA256(code))
  14. {
  15. Byte[] d = System.Text.Encoding.UTF8.GetBytes(text);
  16. Byte[] hmBytes = hmac.ComputeHash(d);
  17. hash = ToHexString(hmBytes);
  18. }
  19. return hash;
  20. }
  21. public static string ToHexString(byte[] array)
  22. {
  23. StringBuilder hex = new StringBuilder(array.Length * 2);
  24. foreach (byte b in array)
  25. {
  26. hex.AppendFormat("{0:x2}", b);
  27. }
  28. return hex.ToString();
  29. }
  30. }
  31. }