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