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.

66 lines
1.4 KiB

using System;
using System.Security.Cryptography;
using System.Text;
namespace EC.Utils.Security
{
/// <summary>
/// md5加密
/// </summary>
public class Md5Helper
{
/// <summary>
/// MD5加密
/// </summary>
/// <param name="str">加密字符</param>
/// <param name="len">加密位数16/32</param>
/// <returns></returns>
public static string Encrypt(string str, int len)
{
string encryptStr;
switch (len)
{
case 16:
encryptStr = Hash(str).Substring(8, 16);
break;
case 32:
encryptStr = Hash(str);
break;
default:
encryptStr = Hash(str);
break;
}
return encryptStr;
}
/// <summary>
/// 32位MD5加密(小写)
/// </summary>
/// <param name="str">输入字段</param>
/// <returns></returns>
public static string Hash(string str)
{
var md5Hasher = new MD5CryptoServiceProvider();
var byteArray = md5Hasher.ComputeHash(Encoding.Default.GetBytes(str));
var builder = new StringBuilder();
foreach (var b in byteArray)
{
builder.Append(b.ToString("x2"));
}
return builder.ToString();
}
/// <summary>
/// 生成编号,形如201008251145409865
/// </summary>
/// <returns></returns>
public static string CreateNo()
{
var randomStr = new Random().Next(1000, 10000).ToString(); //生成编号
var number = DateTime.Now.ToString("yyyyMMddHHmmss") + randomStr;
return number;
}
}
}