简体   繁体   English

MD5文件处理

[英]MD5 file processing

Good morning all, 大家早上好,

I'm working on an MD5 file integrity check tool in C#. 我正在使用C#中的MD5文件完整性检查工具。

How long should it take for a file to be given an MD5 checksum value? 给予文件MD5校验和值需要多长时间? For example, if I try to get a 2gb .mpg file, it is taking around 5 mins+ each time. 例如,如果我尝试获取2gb .mpg文件,则每次大约需要5分钟以上的时间。 This seems overly long. 这似乎过长。

Am I just being impatient? 我只是不耐烦吗?

Below is the code I'm running 下面是我正在运行的代码

public string getHash(String @fileLocation)
    {
        FileStream fs = new FileStream(@fileLocation, FileMode.Open);

        HashAlgorithm alg = new HMACMD5();
        byte[] hashValue = alg.ComputeHash(fs);

        string md5Result = "";

        foreach (byte x in hashValue)
        {
            md5Result += x;
        }           

        fs.Close();           

        return md5Result;           
    }

Any suggestions will be appreciated. 任何建议将不胜感激。

Regards 问候

See this on how to calculate file hash value in a most efficient way. 请参阅此内容 ,了解如何以最有效的方式计算文件哈希值。 You basically have to wrap FileStream into a BufferedStream and than feed that into HMACMD5.ComputeHash(Stream) overload: 基本上,您必须将FileStream包装到BufferedStream然后HMACMD5.ComputeHash(Stream)其输入到HMACMD5.ComputeHash(Stream)重载:

HashAlgorithm hmacMd5 = new HMACMD5();
byte[] hash;

using(Stream fileStream = new FileStream(fileLocation, FileMode.Open))
    using(Stream bufferedStream = new BufferedStream(fileStream, 1200000))
        hash = hmacMd5.ComputeHash(bufferedStream);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM