C#按照字典序升序排序,并使用SHA256生成签名
public string SortEncrypt(Dictionary<string, string> dic) { string strword = ""; var vDic = dic.OrderBy(x => x.Value, new ComparerString()).ToDictionary(x => x.Key, y => y.Value); StringBuilder str = new StringBuilder(); foreach (KeyValuePair<string, string> kv in vDic) { string pkey = kv.Key; string pvalue = kv.Value; strword += pvalue; //str.Append(pkey + "=" + pvalue + "&"); } //dic = dic.OrderBy(h => h.Key).ToDictionary(h => h.Key, h => h.Value); //foreach (string key in dic.Keys) //{ // strword += dic[key]; //} //strword = Helper.Helper.SHA1(strword); string result = SHA.GetSHA256HashFromString(strword); //result = Convert.ToBase64String(Encoding.UTF8.GetBytes(result)); return result; } public static string GetSHA256HashFromString(string strData) { byte[] bytValue = System.Text.Encoding.UTF8.GetBytes(strData); try { SHA256 sha256 = new SHA256CryptoServiceProvider(); byte[] retVal = sha256.ComputeHash(bytValue); StringBuilder sb = new StringBuilder(); for (int i = 0; i < retVal.Length; i++) { sb.Append(retVal[i].ToString("x2")); } return sb.ToString(); } catch (Exception ex) { throw new Exception("GetSHA256HashFromString() fail,error:" + ex.Message); } }
注销部分为一般字典排序