53 lines
1.0 KiB
C#
53 lines
1.0 KiB
C#
using System;
|
|
using System.Linq;
|
|
using System.Security.Cryptography;
|
|
using System.Text;
|
|
|
|
namespace Hua.DotNet.Code.Helper;
|
|
|
|
public static class Md5Helper
|
|
{
|
|
public static string EnCode(string text)
|
|
{
|
|
if (!string.IsNullOrEmpty(text))
|
|
{
|
|
return EnCode(Encoding.Unicode.GetBytes(text));
|
|
}
|
|
return "";
|
|
}
|
|
|
|
public static string EnCode(byte[] data)
|
|
{
|
|
if (data.Length == 0)
|
|
{
|
|
return "";
|
|
}
|
|
try
|
|
{
|
|
MD5CryptoServiceProvider mD5CryptoServiceProvider = new MD5CryptoServiceProvider();
|
|
byte[] inArray = mD5CryptoServiceProvider.ComputeHash(data);
|
|
mD5CryptoServiceProvider.Clear();
|
|
return Convert.ToBase64String(inArray);
|
|
}
|
|
catch
|
|
{
|
|
return "";
|
|
}
|
|
}
|
|
|
|
public static string Md5Sum(string text)
|
|
{
|
|
if (!string.IsNullOrEmpty(text))
|
|
{
|
|
return Md5Sum(Encoding.UTF8.GetBytes(text));
|
|
}
|
|
return "";
|
|
}
|
|
|
|
public static string Md5Sum(byte[] bs)
|
|
{
|
|
return MD5.Create().ComputeHash(bs).Aggregate("", (string current, byte t) => current + Convert.ToString(t, 16).PadLeft(2, '0'))
|
|
.PadLeft(32, '0');
|
|
}
|
|
}
|