MD5簽名

_NickWang發表於2018-06-14

       /// <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; }

相關文章