码迷,mamicode.com
首页 > Windows程序 > 详细

C# 计算文件MD5

时间:2015-04-30 17:33:16      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:

因工作需要对文件进行是否被修改判断,整理的一段生成文件MD5码的代码:

   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:      }

C# 计算文件MD5

标签:

原文地址:http://www.cnblogs.com/leleroyn/p/4469299.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!