using System.Security.Cryptography; using System.Text; namespace DS.Module.Core.Helpers; /// /// /// public class MD5Helper { /// /// MD5加密字符串(32位大写) /// /// 源字符串 /// 加密后的字符串 public static string MD5Encrypt(string source) { MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider(); byte[] bytes = Encoding.UTF8.GetBytes(source); string result = BitConverter.ToString(md5.ComputeHash(bytes)); return result.Replace("-", ""); } /// /// Md5加密 32位小写 /// /// /// public static string Md5EncryptLowerCase(string input) { MD5 md5 = MD5.Create(); byte[] bytes = Encoding.Default.GetBytes(input); byte[] bytesMd5 = md5.ComputeHash(bytes); StringBuilder sb = new StringBuilder(); for (int i = 0; i < bytesMd5.Length; i++) { sb.Append(bytesMd5[i].ToString("x2")); //32位小写 } return sb.ToString(); } /// /// MD5 加密 /// /// 加密文本 /// 是否输出大写加密,默认 false /// 是否输出 16 位 /// public static string Encrypt(string text, bool uppercase = false, bool is16 = false) { byte[] bytes = Encoding.UTF8.GetBytes(text); var data = MD5.HashData(bytes); var stringBuilder = new StringBuilder(); for (var i = 0; i < data.Length; i++) { stringBuilder.Append(data[i].ToString("x2")); } var md5String = stringBuilder.ToString(); var hash = !is16 ? md5String : md5String.Substring(8, 16); return !uppercase ? hash : hash.ToUpper(); } }