using System; using System.Collections.Generic; using System.Linq; using System.Security.Cryptography; using System.Text; using System.Threading.Tasks; namespace JiShe.CollectBus.Common.Encrypt { /// /// 各种加密辅助类 /// public static class EncryptUtil { #region MD5加密 /// /// MD5加密 /// public static string Md5Fun(this string value) { if (value == null) { throw new ArgumentNullException("未将对象引用设置到对象的实例。"); } var encoding = Encoding.UTF8; MD5 md5 = MD5.Create(); return HashAlgorithmBase(md5, value, encoding); } /// /// 加权MD5加密 /// public static string Md5Fun(this string value, string salt) { return salt == null ? value.Md5Fun() : (value + "『" + salt + "』").Md5Fun(); } #endregion /// /// HashAlgorithm 加密统一方法 /// private static string HashAlgorithmBase(HashAlgorithm hashAlgorithmObj, string source, Encoding encoding) { byte[] btStr = encoding.GetBytes(source); byte[] hashStr = hashAlgorithmObj.ComputeHash(btStr); return hashStr.Bytes2Str(); } /// /// 转换成字符串 /// private static string Bytes2Str(this IEnumerable source, string formatStr = "{0:X2}") { StringBuilder pwd = new StringBuilder(); foreach (byte btStr in source) { pwd.AppendFormat(formatStr, btStr); } return pwd.ToString(); } } }