zoukankan      html  css  js  c++  java
  • 计算文件的散列值

    介绍一种使用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;
            }
  • 相关阅读:
    SQL中的max()函数用法
    C#多线程
    C#操作redis
    spring+mybatis 多数据库事务
    实战项目中 :一个业务对多个数据库操作的同步的处理办法(要么都成功,要么都失败)Threadlocal 数据库事务
    redis之数据操作详解
    C# 两个数组取交集/补集 Intersect()
    MySQL创建索引
    MySQL每日执行
    MySQL删除重复数据
  • 原文地址:https://www.cnblogs.com/magicdlf/p/1086682.html
Copyright © 2011-2022 走看看