1234567891011121314151617181920212223242526272829303132 |
- using System;
- using System.Collections.Generic;
- using System.Security.Cryptography;
- using System.Text;
- namespace Bowin.Common.Encoder.HmacSHA256
- {
- public class HMACSHA256Encoder
- {
- public static string Encode(string key, string text)
- {
- string hash;
- Byte[] code = System.Text.Encoding.UTF8.GetBytes(key);
- using (HMACSHA256 hmac = new HMACSHA256(code))
- {
- Byte[] d = System.Text.Encoding.UTF8.GetBytes(text);
- Byte[] hmBytes = hmac.ComputeHash(d);
- hash = ToHexString(hmBytes);
- }
- return hash;
- }
- public static string ToHexString(byte[] array)
- {
- StringBuilder hex = new StringBuilder(array.Length * 2);
- foreach (byte b in array)
- {
- hex.AppendFormat("{0:x2}", b);
- }
- return hex.ToString();
- }
- }
- }
|