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.

59 lines
1.4 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Cryptography;
namespace Infrastructure.Helpers
{
public class Md5Helper
{
//
// 摘要:
// MD5加密
//
// 参数:
// str:
// 加密字符
//
// code:
// 加密位数16/32
public static string Encrypt(string str, int code)
{
string result = string.Empty;
if (code == 16)
{
result = Hash(str).Substring(8, 16);
}
if (code == 32)
{
result = Hash(str);
}
return result;
}
//
// 摘要:
// 32位MD5加密小写
//
// 参数:
// input:
// 输入字段
public static string Hash(string input)
{
MD5CryptoServiceProvider mD5CryptoServiceProvider = new MD5CryptoServiceProvider();
byte[] array = mD5CryptoServiceProvider.ComputeHash(Encoding.Default.GetBytes(input));
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < array.Length; i++)
{
stringBuilder.Append(array[i].ToString("x2"));
}
return stringBuilder.ToString();
}
}
}