110 lines
3.3 KiB
C#
110 lines
3.3 KiB
C#
|
|
using System;
|
|||
|
|
using System.ComponentModel;
|
|||
|
|
using System.Security.Cryptography;
|
|||
|
|
using System.Text;
|
|||
|
|
|
|||
|
|
namespace APT.Infrastructure.Core
|
|||
|
|
{
|
|||
|
|
/// <summary>
|
|||
|
|
/// Hash工具类
|
|||
|
|
/// </summary>
|
|||
|
|
public static class HashUtil
|
|||
|
|
{
|
|||
|
|
/// <summary>
|
|||
|
|
/// 计算字符串MD5值
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="source">要计算的值</param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public static string GetMD5(string source)
|
|||
|
|
{
|
|||
|
|
byte[] bytes = Encoding.UTF8.GetBytes(source);
|
|||
|
|
return GetMD5(bytes);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 计算byte数组MD5值
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="bytes">要计算的byte数组</param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public static string GetMD5(byte[] bytes)
|
|||
|
|
{
|
|||
|
|
StringBuilder builder = new StringBuilder();
|
|||
|
|
MD5 hash = new MD5CryptoServiceProvider();
|
|||
|
|
bytes = hash.ComputeHash(bytes);
|
|||
|
|
foreach (byte b in bytes)
|
|||
|
|
{
|
|||
|
|
builder.AppendFormat("{0:X2}", b);
|
|||
|
|
}
|
|||
|
|
return builder.ToString();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 产生随机字串,可用来自动生成密码,默认长度8位16进制
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="len">长度</param>
|
|||
|
|
/// <param name="type">字串类型 0 字母 1 数字 2 大写字母 3 小写字母 4 十六进制字母 5 混合</param>
|
|||
|
|
/// <param name="addChars">额外字符</param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public static string rand_string(int len = 8, RandomTypeEnum type = RandomTypeEnum.Hex, string addChars = "")
|
|||
|
|
{
|
|||
|
|
string chars = "";
|
|||
|
|
switch (type) {
|
|||
|
|
case RandomTypeEnum.Alphabet:
|
|||
|
|
chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"+addChars;
|
|||
|
|
break;
|
|||
|
|
case RandomTypeEnum.Number:
|
|||
|
|
chars = "0123456789";
|
|||
|
|
break;
|
|||
|
|
case RandomTypeEnum.Uppercase:
|
|||
|
|
chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"+addChars;
|
|||
|
|
break;
|
|||
|
|
case RandomTypeEnum.Lowercase:
|
|||
|
|
chars = "abcdefghijklmnopqrstuvwxyz"+ addChars;
|
|||
|
|
break;
|
|||
|
|
case RandomTypeEnum.Hex:
|
|||
|
|
chars = "0123456789ABCDEF"+addChars;
|
|||
|
|
break;
|
|||
|
|
default :
|
|||
|
|
// 默认去掉了容易混淆的字符oOLl和数字01,要添加请使用addChars参数
|
|||
|
|
chars = "ABCDEFGHIJKMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz23456789"+ addChars;
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
char[] constant = chars.ToCharArray();
|
|||
|
|
System.Text.StringBuilder newRandom = new System.Text.StringBuilder(constant.Length);
|
|||
|
|
Random rd = new Random();
|
|||
|
|
for (int i = 0; i < len; i++)
|
|||
|
|
{
|
|||
|
|
newRandom.Append(constant[rd.Next(constant.Length)]);
|
|||
|
|
}
|
|||
|
|
return newRandom.ToString();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// 随机数类型
|
|||
|
|
/// </summary>
|
|||
|
|
public enum RandomTypeEnum
|
|||
|
|
{
|
|||
|
|
[Description("字母")]
|
|||
|
|
Alphabet = 0,
|
|||
|
|
|
|||
|
|
[Description("数字")]
|
|||
|
|
Number = 1,
|
|||
|
|
|
|||
|
|
[Description("大写字母")]
|
|||
|
|
Uppercase = 2,
|
|||
|
|
|
|||
|
|
[Description("小写字母")]
|
|||
|
|
Lowercase = 3,
|
|||
|
|
|
|||
|
|
[Description("十六进制字母")]
|
|||
|
|
Hex = 4,
|
|||
|
|
|
|||
|
|
[Description("混合")]
|
|||
|
|
Mix = 5
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
}
|