using System;
using System.Collections.Generic;
using System.IO;
using System.Security.Cryptography;
using System.Text;
namespace SteamTool.Core.Common
{
///
/// 类名 :MD5加密
/// 说明 :
///
[Obsolete("use System.Security.Cryptography.Hashs.String(ByteArray).MD5", true)]
public static class Md5Method
{
///
/// 用MD5加密一段字符串
///
/// 字符串
/// 是否小写
/// MD5串
public static string MD5Encrypt(string str, bool isLower = false)
{
using (var md5 = MD5.Create())
{
var result = md5.ComputeHash(Encoding.UTF8.GetBytes(str));
var strResult = BitConverter.ToString(result).Replace("-", "");
return isLower ? strResult.ToLower() : strResult;
}
}
///
/// 用MD5加密流
///
/// 流
/// str
public static byte[] MD5Encrypt(Stream inputStream)
{
System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
return md5.ComputeHash(inputStream);
}
///
/// 用MD5加密字节数组
///
/// 字节数组
///
public static byte[] MD5Encrypt(byte[] myBytes)
{
System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
return md5.ComputeHash(myBytes, 0, myBytes.Length);
}
///
/// 文件路径
///
///
///
public static string FileMD5Encrypt(string path)
{
using FileStream fs = new FileStream(path, FileMode.Open);
MD5CryptoServiceProvider provider = new MD5CryptoServiceProvider();
byte[] data = provider.ComputeHash(fs);
StringBuilder builder = new StringBuilder();
for (int i = 0; i < data.Length; i++)
{
builder.Append(data[i].ToString("X2"));
}
return builder.ToString();
}
}
}