63 lines
1.3 KiB
C#
63 lines
1.3 KiB
C#
using System;
|
|
using System.Security.Cryptography;
|
|
using System.Text;
|
|
|
|
namespace MAX.Core.Cryptography;
|
|
|
|
public class MD5Helper
|
|
{
|
|
/// <summary>
|
|
/// MD5加密
|
|
/// </summary>
|
|
/// <param name="instring"></param>
|
|
/// <returns></returns>
|
|
public static string Md5_32(string instring)
|
|
{
|
|
return _Md532(instring);
|
|
}
|
|
public static string Md5_Lower32(string instring)
|
|
{
|
|
return _Md532(instring, true);
|
|
}
|
|
public static string _Md532(string instring, bool IsLower = false)
|
|
{
|
|
byte[] result = Encoding.UTF8.GetBytes(string.IsNullOrEmpty(instring) ? "" : instring.Trim());
|
|
MD5 md5 = MD5.Create();
|
|
byte[] output = md5.ComputeHash(result);
|
|
string value = BitConverter.ToString(output).Replace("-", "");
|
|
if (IsLower)
|
|
{
|
|
return value.ToLower();
|
|
}
|
|
return value;
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// MD5加密
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public static long GetLong(byte[] result)
|
|
{
|
|
return
|
|
Convert.ToInt64(
|
|
BitConverter.ToString(MD5.Create().ComputeHash(result), 4,
|
|
8).Replace("-", ""), 16);
|
|
}
|
|
|
|
/// <summary>
|
|
/// MD5 16位加密转long
|
|
/// </summary>
|
|
/// <param name="url"></param>
|
|
/// <returns></returns>
|
|
public static long GetLong(string url)
|
|
{
|
|
if (string.IsNullOrEmpty(url))
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
return Convert.ToInt64(BitConverter.ToString(MD5.Create().ComputeHash(Encoding.Default.GetBytes(url)), 4, 8).Replace("-", ""), 16);
|
|
}
|
|
}
|