What is the fastest way to create a checksum for large files in C#

Posted by crono on Stack Overflow See other posts from Stack Overflow or by crono
Published on 2009-07-24T13:20:22Z Indexed on 2010/03/19 16:01 UTC
Read the original article Hit count: 538

Filed under:
|
|
|

Hi,
I have to sync large files across some machines. The files can be up to 6GB in size. The sync will be done manually every few weeks. I cant take the filename into consideration because they can change anytime.

My plan is to create checksums on the destination PC and on the source PC and than copy all files with a checksum, which are not already in the destination, to the destination. My first attempt was something like this:

using System.IO;
using System.Security.Cryptography;

private static string GetChecksum(string file)
{
    using (FileStream stream = File.OpenRead(file))
    {
        SHA256Managed sha = new SHA256Managed();
        byte[] checksum = sha.ComputeHash(stream);
        return BitConverter.ToString(checksum).Replace("-", String.Empty);
    }
}

The Problem was the runtime:
- with SHA256 with a 1,6 GB File -> 20 minutes
- with MD5 with a 1,6 GB File -> 6.15 minutes

Is there a better - faster - way to get the checksum (maybe with a better hash function)?

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET