C # MD5 Digest algorithm, hashing algorithm

Source: Internet
Author: User
Tags md5 digest

MD5 is message-digest algorithm 5 (Information-Digest algorithm 5), which is used to ensure complete and consistent information transmission. is one of the widely used hashing algorithms (also translated digest algorithm, hashing algorithm)

The MD5 algorithm has the following characteristics:

1, compressibility: Any length of data, calculated the length of the MD5 value is fixed.

2, easy to calculate: It is easy to calculate the MD5 value from the original data.

3, anti-modification: Any changes to the original data, even if only 1 bytes modified, the resulting MD5 value is very different.

4, weak anti-collision: known raw data and its MD5 value, it is very difficult to find a data with the same MD5 value (that is, falsification of data).

5, strong anti-collision: To find two different data, so that they have the same MD5 value, is very difficult.

Get file MD5 value by file path

 Public Static stringGetmd5hashfromfile (stringfileName) {            Try{FileStream file=NewFileStream (FileName, FileMode.Open); System.Security.Cryptography.MD5 MD5=NewSystem.Security.Cryptography.MD5CryptoServiceProvider (); byte[] RetVal =Md5.computehash (file); File.                Close (); StringBuilder SB=NewStringBuilder ();  for(inti =0; i < retval.length; i++) {sb. Append (Retval[i]. ToString ("X2")); }                returnsb.            ToString (); }            Catch(Exception ex) {Throw NewException ("getmd5hashfromfile () Fail,error:"+Ex.            Message); }        }

Get MD5 value by flow path

 Public Static stringGetmd5hash (Stream input) {MD5CryptoServiceProvider Md5hasher=NewMD5CryptoServiceProvider (); byte[] data =Md5hasher.computehash (input); Input. Seek (0, Seekorigin.begin); StringBuilder Sbuilder=NewStringBuilder ();  for(inti =0; I < data. Length; i++) {sbuilder.append (Data[i]. ToString ("X2")); }            returnsbuilder.tostring (); }

Test found: through the file stream to get MD5 value, changed the file suffix is no difference.

String MD5 value

 Public Static stringGetmd5hash (stringinput) {            //Create A new instance of the MD5CryptoServiceProvider object.MD5CryptoServiceProvider Md5hasher =NewMD5CryptoServiceProvider (); //Convert The input string to a byte array and compute the hash.            byte[] data =Md5hasher.computehash (Encoding.Default.GetBytes (input)); //Create A new Stringbuilder to collect the bytes//and Create a string.StringBuilder Sbuilder =NewStringBuilder (); //Loop through each byte of the hashed data//and format each one as a hexadecimal string.             for(inti =0; I < data. Length; i++) {sbuilder.append (Data[i]. ToString ("X2")); }            //Return the hexadecimal string.            returnsbuilder.tostring (); }

String MD5 Value Comparison

       //Verify a hash against a string. MD5 value is case- insensitive        Public Static BOOLVerifymd5hash (stringInputstringhash) {            //Hash the input.            stringHashofinput =Getmd5hash (input); //Create A StringComparer an compare the hashes.StringComparer comparer =stringcomparer.ordinalignorecase; if(0==Comparer.compare (Hashofinput, hash)) {return true; }            Else            {                return false; }        }

C # MD5 Digest algorithm, hashing algorithm

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.