[英]How to do a checksum on Windows?
有沒有辦法像在 unix/linux 機器上一樣在 windows 機器上進行校驗和?
我無法下載任何 3rd 方工具,想知道是否有類似的本地工具?
你可以得到文件的MD5 hash。 您基本上會解析文件並將其作為字符串傳遞到此 function 中。 例如
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;
}
(取自這里)
...或作為文件:
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();
}
取自此處計算文件的 MD5 校驗和
對於 windows 的現代版本,您可以運行命令行(就像您提到的“像 Unix 機器等”)。
該工具名為FCIV ,您可以從 Microsoft Here 下載。 https://www.microsoft.com/en-us/download/details.aspx?id=11533
運行它很容易......
FCIV -md5 -sha1 C:\path\to\my\file
MD5 SHA1
------------------------------------------------------------------------------------------
8a3d1ae852c3d2f255ea9a732a539721 9747e6afa6d6fcb94fe3bf86ead91683c26d1aca c:\path\to\my\file
我很確定在 powershell 中,您可以將該響應解析為 PSObject。
這是一個 powershell 1 襯里,如果文件的校驗和匹配,它將返回“真”。 只需根據需要替換 $filePath、$hash 和加密類型。
certUtil -hashfile $filePath SHA256 | Select-String -Pattern '^.{2}\s' | % {($_ -Replace ' ', '') -eq "$hash"}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.