2011-06-29 29 views

回答

1

你可以得到文件的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校验两者的文件

+0

太棒了!谢谢 – user118190

0

对于Windows的现代版本,你可以运行一个命令行(就像你提到的'就像Unix机器'一样)。

该工具被称为FCIV,您可以从Microsoft下载这里。 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 

Im相当肯定,在PowerShell中,你可以解析响应为PSObject。