惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google DeepMind News
Google DeepMind News
罗磊的独立博客
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Last Week in AI
Last Week in AI
云风的 BLOG
云风的 BLOG
T
The Blog of Author Tim Ferriss
Y
Y Combinator Blog
A
About on SuperTechFans
WordPress大学
WordPress大学
B
Blog
Martin Fowler
Martin Fowler
Jina AI
Jina AI
I
InfoQ
P
Proofpoint News Feed
小众软件
小众软件
S
SegmentFault 最新的问题
V
V2EX
B
Blog RSS Feed
量子位
大猫的无限游戏
大猫的无限游戏
aimingoo的专栏
aimingoo的专栏
博客园 - 三生石上(FineUI控件)
MongoDB | Blog
MongoDB | Blog
美团技术团队

博客园 - magicdlf

[HIMCM暑期班]第4课: 扑克牌问题 [HIMCM暑期班]第3课:一个博弈问题 [HIMCM暑期班]第2课:建模 [HIMCM暑期班]第1课:概述 [C#]ASP.NET MVC 3 在线学习资料 [HIMCM]Consortium可以免费下载了! [C#]在Windows Service中使用ThreadPool [HIMCM]MathType小练习 [C#]DataGridView中使用数据绑定Enum类型 SRM 518 解题报告 SRM 522 解题报告 SRM 521 解题报告 [原创]简易版Socket聊天室 附源码 再谈C#扫雷 C#实现扫雷出炉 如何通过P/Invoke返回Struct和String Array zz .NET抽象工厂模式 from cnblogs 清空VSTFS cache 如何在VSTFS中设置email notification
计算文件的散列值
magicdlf · 2008-02-29 · via 博客园 - magicdlf

介绍一种使用md5计算hash值的方法。 下面的代码分别计算两个文件的散列值并比较两个文件是否相同。

using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.IO;

        static bool fileCompare(string srcFilename, string destFilename)
        {
            try
            {
                //if file doesn't exist, will throw exception
                FileInfo srcFile = new FileInfo(srcFilename);
                FileInfo destFile = new FileInfo(destFilename);
                MD5 checksumCalculator = MD5.Create();
                byte[] srcChecksum = checksumCalculator.ComputeHash(srcFile.OpenRead());
                byte[] destChecksum = checksumCalculator.ComputeHash(destFile.OpenRead());
                if (srcChecksum.Length != destChecksum.Length)
                    return false;
                for (int index = 0; index < srcChecksum.Length; index++)
                {
                    if (srcChecksum[index] != destChecksum[index])
                        return false;
                }
                return true;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            return false;
        }