[英]Best way to tell if two files are the same?
If I transfer a file over the internet from computer A to computer B using C# using one of the many ways to do file transfers with .NET, What is the best way to tell if the file on computer A and computer B is the same? 如果我使用C#通过互联网将文件从计算机A传输到计算机B,使用.NET中的一种方法进行文件传输,那么判断计算机A和计算机B上的文件是否相同的最佳方法是什么?
I am thinking that MD5 hashes would be a good way to tell. 我在想MD5哈希是一个很好的方式。 It seems like that is a widely accepted way to tell.
这似乎是一种被广泛接受的方式。 However, I am just double checking to see if there is not a better way out there hidden in the .NET framework.
但是,我只是仔细检查一下,看看.NET框架中是否有更好的方法。
Thank you Tony 谢谢托尼
MD5是要走的路。
CRC32 or Adler32, which are a lot faster then MD5. CRC32或Adler32,比MD5快很多。 You should use MD5 if you need to check if file was manipulated with malicious intent.
如果需要检查文件是否被恶意操纵,则应使用MD5。 If there is no need to, than it's overkill.
如果没有必要,那就太过分了。
SHA2 (SHA256, SHA512) algorithms are better than MD5 for many reasons. 由于多种原因,SHA2(SHA256,SHA512)算法优于MD5。
The native implementations in .NET are SHA256Cng , SHA384Cng and SHA512Cng. .NET中的本机实现是SHA256Cng ,SHA384Cng和SHA512Cng。 Instead of instantiating them explicitly, you can define them as the default algorithm to use when hashing using the < cryptoClass > element in your configuration file.
您可以将它们定义为使用配置文件中的<cryptoClass>元素进行散列时使用的默认算法,而不是显式地实例化它们。
After you do that, you can just write HashAlgorithm.Create()
or SHA256256.Create()
to create the native instance. 执行此操作后,您可以编写
HashAlgorithm.Create()
或SHA256256.Create()
来创建本机实例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.