"Go" "C #" to determine whether two files are the same

Source: Internet
Author: User

Use the System.security.Cryptography.HashAlgorithm class to generate a hash code for each file, and then compare two hash codes for the same

The hashing algorithm generates a small binary "fingerprint" for a file, and from a statistical point of view, different files cannot generate the same hash code

To generate a hash code, you must first create a HashAlgorithm object, which is done by the Hashalgorithm.create method. and then call

Hashalgorithm.computehash method, which returns a byte array that stores the hash code, and then uses bitconverter.tostring () to

Replace with string for comparison.

 Public Static BOOLIsvalidfilecontent (stringFilePath1,stringfilePath2) {            //Create a hash algorithm objectusing(HashAlgorithm hash =hashalgorithm.create ()) {                using(FileStream file1 =NewFileStream (FilePath1, FileMode.Open), file2=NewFileStream (Filepath2,filemode.open)) {                    byte[] hashByte1 = Hash.computehash (file1);//hash algorithm A byte array that hashes the hash code based on the textbyte[] HashByte2 =Hash.computehash (file2); stringSTR1 = bitconverter.tostring (hashByte1);//to assemble a number of bytes into a string stringSTR2 =bitconverter.tostring (HASHBYTE2); return(STR1==STR2);//Compare hash Codes               }            }        } 

"Go" "C #" to determine whether two files are the same

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.