Md5 签名算法

时间:2023-03-08 17:10:33

/// <summary>
        /// MD5签名
        /// </summary>
        /// <param name="prestr">待签名字符串</param>
        /// <param name="key">密钥</param>
        /// <param name="charset">编码</param>
        /// <returns>签名</returns>
        public static string Sign(string prestr, string key, string charset = "utf-8")
        {
            prestr = prestr + "&key=" + key;
            var sb = new StringBuilder(32);
            MD5 md5 = new MD5CryptoServiceProvider();
            var arr = md5.ComputeHash(Encoding.GetEncoding(charset).GetBytes(prestr));
            foreach (var b in arr)
            {
                sb.Append(b.ToString("x").PadLeft(2, '0'));
            }
            return sb.ToString();
        }