using System; using System.Collections.Generic; using System.Linq; using System.Security.Cryptography; using System.Text; using System.Threading.Tasks; namespace DG.Tool { public static class SignHelper { /// /// 计算签名 /// /// /// /// public static string GetSign(string appId, string appSecret, Dictionary param, string timestamps) { //一次排序 var newP = param.OrderBy(m => m.Key).ToDictionary(m => m.Key, n => n.Value); var pJosn = JsonHelper.ToJson(newP); //二次排序 var enStrList = new string[] { appId, pJosn, appSecret, timestamps }; Array.Sort(enStrList, string.CompareOrdinal); //拼接 var enStr = string.Join("", enStrList); //md5 加密 return GetMd5Hash(enStr); } /// /// 计算 md5 /// /// /// private static string GetMd5Hash(string enCode) { string res = ""; byte[] data = Encoding.GetEncoding("utf-8").GetBytes(enCode); MD5 md5 = MD5.Create(); byte[] bytes = md5.ComputeHash(data); for (int i = 0; i < bytes.Length; i++) { res += bytes[i].ToString("x2"); } return res; } /// /// 获取时间戳 /// /// public static string GetTimeStamp() { TimeSpan ts = DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, 0); return Convert.ToInt64(ts.TotalSeconds).ToString(); } } }