/// <summary> /// 获取文件的md5摘要 /// </summary> /// <param name="sFile">文件流</param> /// <returns>MD5摘要结果</returns> public static string GetAbstractToMD5(Stream sFile) { MD5 md5 = new MD5CryptoServiceProvider(); byte[] result = md5.ComputeHash(sFile); StringBuilder sb = new StringBuilder(32); for (int i = 0; i < result.Length; i++) { sb.Append(result[i].ToString("x").PadLeft(2, '0')); } return sb.ToString(); } /// <summary> /// 获取文件的md5摘要 /// </summary> /// <param name="dataFile">文件流</param> /// <returns>MD5摘要结果</returns> public static string GetAbstractToMD5(byte[] dataFile) { MD5 md5 = new MD5CryptoServiceProvider(); byte[] result = md5.ComputeHash(dataFile); StringBuilder sb = new StringBuilder(32); for (int i = 0; i < result.Length; i++) { sb.Append(result[i].ToString("x").PadLeft(2, '0')); } return sb.ToString(); } /// <summary> /// 签名字符串 /// </summary> /// <param name="prestr">需要签名的字符串</param> /// <param name="key">密钥</param> /// <param name="_input_charset">编码格式</param> /// <returns>签名结果</returns> public static string Md5Sign(string prestr, string key, string _input_charset) { StringBuilder sb = new StringBuilder(32); prestr = prestr + key; MD5 md5 = new MD5CryptoServiceProvider(); byte[] t = md5.ComputeHash(Encoding.GetEncoding(_input_charset).GetBytes(prestr)); for (int i = 0; i < t.Length; i++) { sb.Append(t[i].ToString("x").PadLeft(2, '0')); } return sb.ToString(); } /// <summary> /// 验证签名 /// </summary> /// <param name="prestr">需要签名的字符串</param> /// <param name="sign">签名结果</param> /// <param name="key">密钥</param> /// <param name="_input_charset">编码格式</param> /// <returns>验证结果</returns> public static bool Md5Verify(string prestr, string sign, string key, string _input_charset) { string mysign = Md5Sign(prestr, key, _input_charset); if (mysign == sign) { return true; } else { return false; } }