Step1:引入库
using System.Security.Cryptography;
using System.IO;
Step2: 计算字符串的Md5值
static public string GetMD5WithString(string sDataIn)
{
string str = "";
byte[] data = Encoding.GetEncoding("utf-8").GetBytes(str);
MD5 md5 = new MD5CryptoServiceProvider();
byte[] bytes = md5.ComputeHash(data);
for (int i = ; i < bytes.Length; i++)
{
str += bytes[i].ToString("x2");
}
return str;
}
Step3:计算文件的Md5值
static public string GetMD5WithFilePath(string filePath)
{
FileStream file = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
byte[] hash_byte = md5.ComputeHash(file);
string str = System.BitConverter.ToString(hash_byte);
str = str.Replace("-", "");
return str;
}