有没有办法像在 unix/linux 机器上一样在 windows 机器上执行校验和?
我无法下载任何第 3 方工具,想知道本地是否有类似的东西?
最佳答案
您可以获得文件的 MD5 哈希值。您基本上会解析文件并将其作为字符串传递给此函数。例如
public string GetMD5Hash(string input)
{
System.Security.Cryptography.MD5CryptoServiceProvider x = new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] bs = System.Text.Encoding.UTF8.GetBytes(input);
bs = x.ComputeHash(bs);
System.Text.StringBuilder s = new System.Text.StringBuilder();
foreach (byte b in bs)
{
s.Append(b.ToString("x2").ToLower());
}
string password = s.ToString();
return password;
}
(取自here)
...或作为文件:
protected string GetMD5HashFromFile(string fileName)
{
FileStream file = new FileStream(fileName, FileMode.Open);
MD5 md5 = new MD5CryptoServiceProvider();
byte[] retVal = md5.ComputeHash(file);
file.Close();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < retVal.Length; i++)
{
sb.Append(retVal[i].ToString("x2"));
}
return sb.ToString();
}
取自here计算文件的MD5校验和
关于windows - 如何在 Windows 上执行校验和?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6523924/