LASAPlatform/Infrastructure/Helpers/Md5.cs

47 lines
1.2 KiB
C#
Raw Normal View History

2025-04-23 09:19:29 +08:00
using System;
using System.Security.Cryptography;
using System.Text;
namespace Infrastructure.Helpers
{
public class Md5
{
2025-06-17 10:13:59 +08:00
/// <summary>
2025-06-18 15:49:44 +08:00
/// 计算字节流的MD5值 计算md5
2025-06-17 10:13:59 +08:00
/// </summary>
public static string CalculateStreamMd5(Stream stream)
{
if (stream == null)
throw new ArgumentNullException(nameof(stream));
if (!stream.CanRead)
throw new InvalidOperationException("流不可读");
using var md5 = MD5.Create();
var hashBytes = md5.ComputeHash(stream);
return BitConverter.ToString(hashBytes).Replace("-", "").ToLower();
}
2025-04-23 09:19:29 +08:00
public static string Encrypt(string str)
{
string pwd = String.Empty;
MD5 md5 = MD5.Create();
// 编码UTF8/Unicode 
byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(str));
// 转换成字符串
for (int i = 0; i < s.Length; i++)
{
//格式后的字符是小写的字母
//如果使用大写X则格式后的字符是大写字符
pwd = pwd + s[i].ToString("X");
}
return pwd;
}
}
}