How to do a checksum on Windows?
Is there a way to do a checksum on windows machines like on unix/li开发者_运维知识库nux machines?
I cannot download any 3rd party tools and wondering if there was something similar natively?
You can get get the MD5 hash of the file. You would essentially parse the file and pass it into this function as a string. e.g.
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;
}
(taken from here)
...or as a file:
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();
}
Taken from here Calculate MD5 Checksum for File
For modern versions of windows, you can run a command line (much like you mentioned 'Like Unix machines, etc').
The tool is called FCIV and you can download from Microsoft Here. https://www.microsoft.com/en-us/download/details.aspx?id=11533
To Run it its pretty easy...
FCIV -md5 -sha1 C:\path\to\my\file
MD5 SHA1
------------------------------------------------------------------------------------------
8a3d1ae852c3d2f255ea9a732a539721 9747e6afa6d6fcb94fe3bf86ead91683c26d1aca c:\path\to\my\file
Im pretty sure that in powershell you can parse that response as a PSObject.
Here's a powershell 1 liner that will return 'true' if the file's checksum matches. Just replace $filePath, $hash, and encryption type as appropriate.
certUtil -hashfile $filePath SHA256 | Select-String -Pattern '^.{2}\s' | % {($_ -Replace ' ', '') -eq "$hash"}
精彩评论