/// <summary> /// 获取签名字符串 /// </summary> /// <param name="dic">待签名参数</param> /// <param name="key">签名密钥</param> /// <param name="signType">签名加密方法</param> /// <returns></returns> public static string GetSign(SortedDictionary <string, object> dic, string key, SignType signType) { var strSign = string.Empty; var strRequestData = string.Empty; StringBuilder sbRequest = new StringBuilder(); foreach (var item in dic) { if (item.Key.ToLower() != "sign" && item.Key.ToLower() != "sign_type" && item.Value != "" && item.Value != null) { sbRequest.AppendFormat("{0}={1}&", item.Key, item.Value); } } if (sbRequest.Length > 0) { if (!string.IsNullOrEmpty(key)) { strRequestData = string.Format("{0}&key={1}", sbRequest.ToString(), key.Trim()); } else { strRequestData = sbRequest.ToString().TrimEnd('&'); } } switch (signType) { case SignType.MD5: { strSign = MD5Util.Sign(strRequestData, "").ToUpper(); break; }; case SignType.SHA1: { strSign = SHAEncryptUtil.SHA1Encrypt(strRequestData); break; }; case SignType.SHA256: { strSign = SHAEncryptUtil.SHA256Encrypt(strRequestData); break; } default: break; } return(strSign); }
/// <summary> /// 获取【美团】签名字符串 /// </summary> /// <param name="dic">待签名参数</param> /// <param name="key">签名密钥</param> /// <returns></returns> public static string GetMeituanSign(SortedDictionary <string, object> dic, string key) { var sbRequest = new StringBuilder(); //签名密钥 if (!string.IsNullOrEmpty(key)) { sbRequest.AppendFormat(key.Trim()); } //请求参数 foreach (var item in dic) { if (item.Key.ToLower() != "sign" && item.Key.ToLower() != "sign_type" && item.Value != "" && item.Value != null) { sbRequest.AppendFormat("{0}{1}", item.Key, item.Value); } } var strSign = SHAEncryptUtil.SHA1Encrypt(sbRequest.ToString()).ToLower(); return(strSign); }