You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
49 lines
1.5 KiB
49 lines
1.5 KiB
using System.Security.Cryptography;
|
|
using System.Text;
|
|
|
|
namespace RS.Common
|
|
{
|
|
/// <summary>
|
|
/// MD5加密
|
|
/// </summary>
|
|
public class Md5Util
|
|
{
|
|
/// <summary>
|
|
/// MD5加密
|
|
/// </summary>
|
|
/// <param name="str">加密字符</param>
|
|
/// <param name="code">加密位数16/32</param>
|
|
/// <returns></returns>
|
|
public static string Encrypt(string str, int code = 32)
|
|
{
|
|
HashAlgorithm provider = CryptoConfig.CreateFromName("MD5") as HashAlgorithm;
|
|
if (string.IsNullOrEmpty(str)) return string.Empty;
|
|
|
|
byte[] bytes = Encoding.UTF8.GetBytes(str);
|
|
byte[] hashValue = provider.ComputeHash(bytes);
|
|
StringBuilder sb = new StringBuilder();
|
|
switch (code)
|
|
{
|
|
case 16://16位密文是32位密文的9到24位字符
|
|
for (int i = 4; i < 12; i++)
|
|
sb.Append(hashValue[i].ToString("x2"));
|
|
break;
|
|
|
|
case 32:
|
|
for (int i = 0; i < 16; i++)
|
|
{
|
|
sb.Append(hashValue[i].ToString("x2"));
|
|
}
|
|
break;
|
|
|
|
default:
|
|
for (int i = 0; i < hashValue.Length; i++)
|
|
{
|
|
sb.Append(hashValue[i].ToString("x2"));
|
|
}
|
|
break;
|
|
}
|
|
return sb.ToString();
|
|
}
|
|
}
|
|
} |