MD5签名

       /// <summary>
        /// 校验签名
        /// </summary>
        /// <param name="model">实体</param>
        /// <param name="sign">签名</param>
        /// <returns></returns>
        public static bool CheckSign(object model, string sign)
        {
            SortedDictionary<string, string> signParams = new SortedDictionary<string, string>(StringComparer.Ordinal);
            var keyValueTo = ToMap(model);
            foreach (var p in keyValueTo)
            {
                if (p.Key != "Sign" && p.Key != "Signlen" && p.Key != "Img")
                {
                    signParams[p.Key] = p.Value;
                }
            }
            StringBuilder paramStr = new StringBuilder();
            foreach (var item in signParams)
            {
                if (string.IsNullOrWhiteSpace(item.Value) == false)
                {
                    paramStr.AppendFormat("{0}={1}&", item.Key, item.Value);
                }
            }
            paramStr.AppendFormat("Key={0}", api_secret);
            string computeSign = GetMd5Hash(paramStr.ToString()).ToUpper();
            return string.Equals(sign, computeSign);
        }
        /// <summary>
        /// 生成签名
        /// </summary>
        /// <param name="model">实体</param>
        /// <returns></returns>
        public static string GetSign(object model)
        {
            SortedDictionary<string, string> signParams = new SortedDictionary<string, string>(StringComparer.Ordinal);
            var keyValueTo = ToMap(model);
            foreach (var p in keyValueTo)
            {
                if (p.Key != "Sign" && p.Key != "Signlen" && p.Key != "Img")
                {
                    signParams[p.Key] = p.Value;
                }
            }
            StringBuilder paramStr = new StringBuilder();
            foreach (var item in signParams)
            {
                if (string.IsNullOrWhiteSpace(item.Value) == false)
                {
                    paramStr.AppendFormat("{0}={1}&", item.Key, item.Value);
                }
            }
            paramStr.AppendFormat("Key={0}", api_secret);
            string computeSign = GetMd5Hash(paramStr.ToString()).ToUpper();
            return computeSign;
        }

        /// <summary>  
        /// 将对象属性转换为key-value对  
        /// </summary>  
        /// <param name="o"></param>  
        /// <returns></returns>  
        private static Dictionary<String, String> ToMap(Object o)
        {
            Dictionary<String, String> map = new Dictionary<String, String>();

            Type t = o.GetType();

            PropertyInfo[] pi = t.GetProperties(BindingFlags.Public | BindingFlags.Instance);

            foreach (PropertyInfo p in pi)
            {
                MethodInfo mi = p.GetGetMethod();

                if (mi != null && mi.IsPublic)
                {
                    map.Add(p.Name, mi.Invoke(o, new String[] { }).ToString());
                }
            }

            return map;

        }
        /// <summary>
        /// MD5加密
        /// </summary>
        /// <param name="input">字符串</param>
        /// <returns></returns>
        public static string GetMd5Hash(string input)
        {
            // Create a new instance of the MD5CryptoServiceProvider object.

            MD5 md5Hasher = MD5.Create();

            // Convert the input string to a byte array and compute the hash.

            byte[] data = md5Hasher.ComputeHash(Encoding.UTF8.GetBytes(input));

            // Create a new Stringbuilder to collect the bytes

            // and create a string.

            StringBuilder sBuilder = new StringBuilder();

            // Loop through each byte of the hashed data

            // and format each one as a hexadecimal string.

            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }

            // Return the hexadecimal string.

            return sBuilder.ToString();
        }

        public static string api_secret { get; set; }

posted @ 2018-06-14 11:40 _NickWang 阅读(...) 评论(...) 编辑 收藏