C#計算ファイルMD 5

4084 ワード

作業によりファイルが修正されたか否かの判断を行い、整理されたファイルMD 5コードを生成するコード:
   1:  public class FileHelper
   2:      {
   3:          /// <summary>
   4:          ///  MD5 
   5:          /// </summary>
   6:          public static string MD5Stream(Stream stream)
   7:          {
   8:              MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
   9:              md5.ComputeHash(stream); 
  10:              byte[] b = md5.Hash;
  11:              md5.Clear();
  12:              StringBuilder sb = new StringBuilder(32);
  13:              for (int i = 0; i < b.Length; i++)
  14:              {
  15:                  sb.Append(b[i].ToString("X2"));
  16:              }
  17:              return sb.ToString();
  18:          }
  19:          /// <summary>
  20:          ///  MD5 
  21:          /// </summary>
  22:          public static string MD5Stream(string filePath)
  23:          {
  24:              using (FileStream stream = File.Open(filePath, FileMode.Open))
  25:              {
  26:                  return MD5Stream(stream); 
  27:              }
  28:          }
  29:      }


.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }