C # Implementation MD5

Source: Internet
Author: User

What is MD5?

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 in the computer, the mainstream programming language has been widely MD5 implemented.

Features of MD5:

    • Compressibility: Any length of data, the calculated length of the MD5 value is fixed.
    • Easy to calculate: It is easy to calculate the MD5 value from the original data.
    • Anti-modification: Make any changes to the original data, even if only 1 bytes are modified, the resulting MD5 value is very different.
    • Strong 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).
1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.IO;4 usingSystem.Linq;5 usingSystem.Security.Cryptography;6 usingSystem.Text;7 usingSystem.Threading.Tasks;8 9 namespaceConsoleApplication1Ten { One      Public classMd5helper A     { -         /// <summary> -         ///computes the MD5 value of a byte array the         /// </summary> -         /// <param name= "buffer" ></param> -         /// <returns></returns> -          Public Static stringCalcMD5 (byte[] buffer) +         { -             using(MD5 MD5 =MD5. Create ()) +             { A                 byte[] Md5bytes =md5.computehash (buffer); at                 returnbytestostring (md5bytes); -             } -         } -  -         /// <summary> -         ///turns the resulting MD5 byte array into a string in         /// </summary> -         /// <param name= "md5bytes" ></param> to         /// <returns></returns> +         Private Static stringBytestostring (byte[] md5bytes) -         { theStringBuilder SB =NewStringBuilder (); *              for(inti =0; i < md5bytes.length; i++) $             {Panax NotoginsengSb. Append (Md5bytes[i]. ToString ("X2")); -             } the             returnsb. ToString (); +         } A  the         /// <summary> +         ///computes the MD5 value of a string -         /// </summary> $         /// <param name= "str" ></param> $         /// <returns></returns> -          Public Static stringCalcMD5 (stringstr) -         { the             byte[] buffer =Encoding.UTF8.GetBytes (str); -             returnCalcMD5 (buffer);Wuyi         } the  -         /// <summary> Wu         ///calculates the MD5 value of a stream -         /// </summary> About         /// <param name= "stream" ></param> $         /// <returns></returns> -          Public Static stringCalcMD5 (Stream stream) -         { -             using(MD5 MD5 =MD5. Create ()) A             { +                 byte[] buffer =Md5.computehash (stream); the                 returnbytestostring (buffer); -             } $         } the     } the}

C # Implementation MD5

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.