using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Security.Cryptography; using System.Text; using System.Threading.Tasks; namespace DH.RBAC.Utility.Other { public static class MD5Helper { /// /// 字符串MD5加密。 /// /// 需要加密的字符串 /// public static string md5(this string text) { return md5(text, Encoding.Default); } public static string MD5(this string text) { return MD5(text, Encoding.Default); } /// /// 字符串MD5加密。 /// /// 需要加密的字符串 /// public static string md5(this string text, Encoding encoder) { // Create a new instance of the MD5CryptoServiceProvider object. System.Security.Cryptography.MD5 md5Hasher = System.Security.Cryptography.MD5.Create(); // Convert the input string to a byte array and compute the hash. byte[] data = md5Hasher.ComputeHash(encoder.GetBytes(text)); // 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().ToLower(); } public static string MD5(this string text, Encoding encoder) { return md5(text, encoder).ToUpper(); } /// /// 文件流MD5加密。 /// /// 需要加密的文件流 /// public static string md5(this Stream stream) { MD5 md5serv = MD5CryptoServiceProvider.Create(); byte[] buffer = md5serv.ComputeHash(stream); StringBuilder sb = new StringBuilder(); foreach (byte var in buffer) { sb.Append(var.ToString("x2")); } return sb.ToString().ToLower(); } public static string MD5(this Stream stream) { return md5(stream).ToUpper(); } #region MD5加密 /// /// 字符串MD5加密。 /// /// 需要加密的字符串 /// public static string MD5Encrypt(this string text) { // Create a new instance of the MD5CryptoServiceProvider object. System.Security.Cryptography.MD5 md5Hasher = System.Security.Cryptography.MD5.Create(); // Convert the input string to a byte array and compute the hash. byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(text)); // 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(); } /// /// 文件流MD5加密。 /// /// 需要加密的文件流 /// public static string MD5Encrypt(this Stream stream) { MD5 md5serv = MD5CryptoServiceProvider.Create(); byte[] buffer = md5serv.ComputeHash(stream); StringBuilder sb = new StringBuilder(); foreach (byte var in buffer) { sb.Append(var.ToString("x2")); } return sb.ToString(); } #endregion } }