随笔 - 102  文章 - 0 评论 - 65 阅读 - 20万
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

一、前言

       MD5验证主要用于更新文件功能方面,服务器告知客户端要下载哪些更新文件并提供给客户端其MD5值,客户端从服务器将更新文件下载到本地并计算下载文件的MD5值,将本地接收的MD5值与服务器提供的MD5值进行比对,如果相同则说明下载的文件与服务器提供的文件是一致的,如果不相同则说明下载后文件可能有缺失,应丢弃或断点续传。

 

二、计算文件的MD5值

复制代码
using System.Security.Cryptography;
using System.Text;

using(FileStream file = new FileStream(FilePath, System.IO.FileMode.Open))
{
       MD5 md5 = new MD5CryptoServiceProvider();
       byte[] YourFile = md5.ComputeHash(file);
       file.Close();
       StringBuilder FileMD5 = new StringBuilder();
       for (int i = 0; i < YourFile.Length; i++)
       {
          FileMD5.Append(YourFile[i].ToString("x2"));
       }
       FileMD5.ToString();
}
复制代码

 

posted on   airforce094  阅读(1453)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示