首页 > 技术文章 > c# 计算字符串和文件的MD5值的方法

RTdo 2015-05-07 05:52 原文

快速使用Romanysoft LAB的技术实现 HTML 开发Mac OS App,并销售到苹果应用商店中。
 
HTML开发Mac OS App 视频教程》
 
官方QQ群:(申请加入,说是我推荐的
  • App实践出真知 434558944       App实践出真知
  • App学习交流 452180823          App实践出真知
 
Step1:引入库
1 using System.Security.Cryptography;
2 using System.IO;

 

Step2: 计算字符串的Md5值    

 1         static public string GetMD5WithString(string sDataIn)
 2         {
 3             string str = "";
 4             byte[] data = Encoding.GetEncoding("utf-8").GetBytes(str);
 5             MD5 md5 = new MD5CryptoServiceProvider();
 6             byte[] bytes = md5.ComputeHash(data);
 7             for (int i = 0; i < bytes.Length; i++)
 8             {
 9                 str += bytes[i].ToString("x2");
10             }
11             return str;
12         }

 

Step3:计算文件的Md5值

 

1         static public string GetMD5WithFilePath(string filePath)
2         {
3             FileStream file = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
4             MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
5             byte[] hash_byte = md5.ComputeHash(file);
6             string str = System.BitConverter.ToString(hash_byte);
7             str = str.Replace("-", "");
8             return str;
9         }

 

 

推荐阅读