2025-03-28 09:49:56 +08:00

64 lines
1.9 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace EShang.Common
{
public class MD5Util
{
/// <summary>
/// 获取大写的MD5签名结果
/// </summary>
/// <param name="encypStr">需要签名的串</param>
/// <param name="charset">编码</param>
/// <returns>返回大写的MD5签名结果</returns>
public static string GetMD5(string encypStr, string charset)
{
string retStr;
MD5CryptoServiceProvider m5 = new MD5CryptoServiceProvider();
//创建md5对象
byte[] inputBye;
byte[] outputBye;
//使用GB2312编码方式把字符串转化为字节数组
try
{
inputBye = Encoding.GetEncoding(charset).GetBytes(encypStr);
}
catch (Exception ex)
{
inputBye = Encoding.GetEncoding("GB2312").GetBytes(encypStr);
Console.WriteLine(ex);
}
outputBye = m5.ComputeHash(inputBye);
retStr = System.BitConverter.ToString(outputBye);
retStr = retStr.Replace("-", "").ToUpper();
return retStr;
}
#region -> MD5转译
/// <summary>
/// MD5转译
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string GetMD5(string str)
{
MD5 _MD5 = new MD5CryptoServiceProvider();
byte[] t = _MD5.ComputeHash(Encoding.GetEncoding("utf-8").GetBytes(str));
StringBuilder sb = new StringBuilder(32);
for (int i = 0; i < t.Length; i++)
{
sb.Append(t[i].ToString("x").PadLeft(2, '0'));
}
return sb.ToString();
}
#endregion
}
}