44 lines
1.3 KiB
C#
44 lines
1.3 KiB
C#
using System.Security.Cryptography;
|
|
using System.Text;
|
|
|
|
namespace MasstransferCommon.Utils;
|
|
|
|
/// <summary>
|
|
/// MD5 加密工具类
|
|
/// </summary>
|
|
public class Md5Util
|
|
{
|
|
public static string Md5(string input)
|
|
{
|
|
// 创建一个MD5对象
|
|
// 将输入字符串转换为字节数组
|
|
var inputBytes = Encoding.UTF8.GetBytes(input);
|
|
// 计算输入字节数组的哈希值
|
|
var bytes = MD5.HashData(inputBytes);
|
|
// 将字节数组转换为字符串
|
|
var hashString = new StringBuilder();
|
|
foreach (var b in bytes) hashString.Append($"{b:x2}");
|
|
|
|
return hashString.ToString();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 计算压缩包md5
|
|
/// </summary>
|
|
/// <param name="filePath"></param>
|
|
/// <returns></returns>
|
|
public static string Md5ForZipFile(string filePath)
|
|
{
|
|
using (var md5 = MD5.Create())
|
|
{
|
|
using (var stream = File.OpenRead(filePath))
|
|
{
|
|
// 计算文件流的哈希值
|
|
var hash = md5.ComputeHash(stream);
|
|
// 将哈希值转换为字符串格式
|
|
var hashString = BitConverter.ToString(hash).Replace("-", string.Empty).ToLowerInvariant();
|
|
return hashString;
|
|
}
|
|
}
|
|
}
|
|
} |