using System.Security.Cryptography; using System.Text; namespace U8Server.Util { // // 摘要: // MD5 加密 public static class MD5Encryption { // // 摘要: // MD5 比较 // // 参数: // text: // 加密文本 // // hash: // MD5 字符串 // // uppercase: // 是否输出大写加密,默认 false // // is16: // 是否输出 16 位 // // 返回结果: // bool public static bool Compare(string text, string hash, bool uppercase = false, bool is16 = false) { return Compare(Encoding.UTF8.GetBytes(text), hash, uppercase, is16); } // // 摘要: // MD5 加密 // // 参数: // text: // 加密文本 // // uppercase: // 是否输出大写加密,默认 false // // is16: // 是否输出 16 位 public static string Encrypt(string text, bool uppercase = false, bool is16 = false) { return Encrypt(Encoding.UTF8.GetBytes(text), uppercase, is16); } // // 摘要: // MD5 加密 // // 参数: // bytes: // 字节数组 // // uppercase: // 是否输出大写加密,默认 false // // is16: // 是否输出 16 位 public static string Encrypt(byte[] bytes, bool uppercase = false, bool is16 = false) { byte[] array = MD5.HashData(bytes); StringBuilder stringBuilder = new StringBuilder(); for (int i = 0; i < array.Length; i++) { stringBuilder.Append(array[i].ToString("x2")); } string text = stringBuilder.ToString(); string text2 = ((!is16) ? text : text.Substring(8, 16)); if (uppercase) { return text2.ToUpper(); } return text2; } // // 摘要: // MD5 比较 // // 参数: // bytes: // 字节数组 // // hash: // MD5 字符串 // // uppercase: // 是否输出大写加密,默认 false // // is16: // 是否输出 16 位 // // 返回结果: // bool public static bool Compare(byte[] bytes, string hash, bool uppercase = false, bool is16 = false) { string value = Encrypt(bytes, uppercase, is16); return hash.Equals(value, StringComparison.OrdinalIgnoreCase); } } }