using System.IO;
using System.Security.Cryptography;
using System.Text;
/// <summary>
/// 文件MD5计算器
/// </summary>
public class FileMD5Helper
{
#region 公共方法
/// <summary>
/// 计算文件的md5值,返回大写格式
/// </summary>
/// <param name="url"> 如Application.streamingAssetsPath + "/"+ "mydatabase.sqlite"</param>
/// <returns></returns>
public static string GenerateFileMD5Upper(string url)
{
if (File.Exists(url) == false)
return string.Empty;
byte[] fileByte = File.ReadAllBytes(url);
if (fileByte == null)
return string.Empty;
byte[] hashByte = new MD5CryptoServiceProvider().ComputeHash(fileByte);
return byteArrayToString(hashByte);
}
#endregion
#region 1.获得字符串md5值
public static string GetMD5(string msg)
{
StringBuilder sb = new StringBuilder();
using (MD5 md5 = MD5.Create())
{
byte[] buffer = Encoding.UTF8.GetBytes(msg);
byte[] newB = md5.ComputeHash(buffer);
foreach (byte item in newB)
{
sb.Append(item.ToString("x2"));
}
}
return sb.ToString();
}
#endregion
#region 2获得一个文件的MD5
public static string GetFileMD5(string filepath)
{
StringBuilder sb = new StringBuilder();
using (MD5 md5 = MD5.Create())
{
using (FileStream fs = File.OpenRead(filepath))
{
byte[] newB = md5.ComputeHash(fs);
foreach (byte item in newB)
{
sb.Append(item.ToString("x2"));
}
}
}
return sb.ToString();
}
#endregion
#region 私有方法
/// <summary>
/// 输出数据的十六进制字符串
/// </summary>
/// <param name="arrInput"></param>
/// <returns></returns>
private static string byteArrayToString(byte[] arrInput)
{
StringBuilder sOutput = new StringBuilder(arrInput.Length);
for (int i = 0; i < arrInput.Length; i++)
{
sOutput.Append(arrInput[i].ToString("X2"));
}
return sOutput.ToString();
}
#endregion
}
using System.Security.Cryptography;
using System.Text;
/// <summary>
/// 文件MD5计算器
/// </summary>
public class FileMD5Helper
{
#region 公共方法
/// <summary>
/// 计算文件的md5值,返回大写格式
/// </summary>
/// <param name="url"> 如Application.streamingAssetsPath + "/"+ "mydatabase.sqlite"</param>
/// <returns></returns>
public static string GenerateFileMD5Upper(string url)
{
if (File.Exists(url) == false)
return string.Empty;
byte[] fileByte = File.ReadAllBytes(url);
if (fileByte == null)
return string.Empty;
byte[] hashByte = new MD5CryptoServiceProvider().ComputeHash(fileByte);
return byteArrayToString(hashByte);
}
#endregion
#region 1.获得字符串md5值
public static string GetMD5(string msg)
{
StringBuilder sb = new StringBuilder();
using (MD5 md5 = MD5.Create())
{
byte[] buffer = Encoding.UTF8.GetBytes(msg);
byte[] newB = md5.ComputeHash(buffer);
foreach (byte item in newB)
{
sb.Append(item.ToString("x2"));
}
}
return sb.ToString();
}
#endregion
#region 2获得一个文件的MD5
public static string GetFileMD5(string filepath)
{
StringBuilder sb = new StringBuilder();
using (MD5 md5 = MD5.Create())
{
using (FileStream fs = File.OpenRead(filepath))
{
byte[] newB = md5.ComputeHash(fs);
foreach (byte item in newB)
{
sb.Append(item.ToString("x2"));
}
}
}
return sb.ToString();
}
#endregion
#region 私有方法
/// <summary>
/// 输出数据的十六进制字符串
/// </summary>
/// <param name="arrInput"></param>
/// <returns></returns>
private static string byteArrayToString(byte[] arrInput)
{
StringBuilder sOutput = new StringBuilder(arrInput.Length);
for (int i = 0; i < arrInput.Length; i++)
{
sOutput.Append(arrInput[i].ToString("X2"));
}
return sOutput.ToString();
}
#endregion
}