• 计算文件的散列值


    介绍一种使用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;
            }
  • 相关阅读:
    java @param参数注解
    java 泛型类
    HttpServletRequestWrapper的使用
    java工具类系列 (四.SerializationUtils)
    spring aop通过joinpoint传递参数
    java retention注解
    stringUtils是apache下的Java jar补充包
    slf4j日志系统
    支付宝支付接口开发
    wifi定位原理
  • 原文地址:https://www.cnblogs.com/magicdlf/p/1086682.html
Copyright © 2020-2023  润新知