1.生成的MD5字符串,这些字母表示的是16进制数,因此不区分大小写,所以大写小写都可以
2.item.ToString("x2")) 中的x2 --> x表示小写,X表示大写,2表示用两位进行格式化
如果两个数10和26,正常情况十六进制显示0xA、0x1A,这样看起来不整齐,为了好看,我们可以指定X2,这样显示出来就是:0x0A、0x1A。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography;
using System.IO;
#region 获得一个文件的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 获得字符串的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