Best way to find out if two files match?

If I transfer the file over the Internet from computer A to computer B using C # using one of the many ways to transfer files using .NET. What is the best way to find out if the file on computer A and computer B is the same?

I think MD5 hashes will be a good way to tell. This seems like a common way to tell. However, I just double-check to see if there is a better way hidden in the .NET platform.

Thanks. Tony

+2
c # file
source share
3 answers

MD5 is the way to go.

+7
source share

CRC32 or Adler32, which are much faster than MD5. You should use MD5 if you need to check if the file was manipulated by malicious intentions. If not necessary, than is unnecessary.

+3
source share

SHA2 (SHA256, SHA512) algorithms are better than MD5 for many reasons.

  • They are more resistant to collisions, which is very important for large files. Although MD5 may detect changes to the contents of a file, it is more likely that two large files may have the same MD5.
  • They are BASED for calculation. It may seem strange, but the SHA algorithm is accelerated both on chipsets and on OS implementations. Algorithms themselves are easier to parallelize. As a result, native implementations of SHA or SHA2 algorithms in Vista + are much faster than the equivalent MD5 algorithm.
  • They use a larger block size, which means that they can work with large file blocks at a time. I / O time can add up when processing large files.

Native implementations in .NET: SHA256Cng , SHA384Cng, and SHA512Cng. Instead of creating them explicitly, you can define them as the default algorithm that will be used when hashing with <cryptoClass> in your configuration file.

After that, you can simply write HashAlgorithm.Create() or SHA256256.Create() to create your own instance.

+2
source share

All Articles