You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

66 lines
2.0 KiB
C#

1 year ago
using System.Security.Cryptography;
using System.Text;
namespace DS.Module.Core.Helpers;
/// <summary>
///
/// </summary>
public class MD5Helper
{
/// <summary>
/// MD5加密字符串32位大写
/// </summary>
/// <param name="source">源字符串</param>
/// <returns>加密后的字符串</returns>
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("-", "");
}
11 months ago
/// <summary>
/// Md5加密 32位小写
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
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();
}
/// <summary>
/// MD5 加密
/// </summary>
/// <param name="text">加密文本</param>
/// <param name="uppercase">是否输出大写加密,默认 false</param>
/// <param name="is16">是否输出 16 位</param>
/// <returns></returns>
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();
}
1 year ago
}