A message digest algorithm, such as a digest algorithm for passwords, is often used in programs. But in. NET is slightly more complicated, sometimes we want to do it in a simpler way, and the following code simplifies this process: using System; Using System.Collections.Generic; Using System.Text; Using System.Security.Cryptography; Namespace oohacker.example ... { public class mymessagedigest { //========================================================================= // returns a 40-bit SHA-1 encryption string //============================================== =========================== public static string hexsha (string text) { return makedigesttemplate (SHA1). Create (), text); } //=========================================================== ============== // Returns an MD5 encrypted string with a length of 32 bits //========================================================================= public static string &NBSP;HEXMD5 (String text) { return makedigesttemplate (MD5). Create (), text); } //=========================================================== ============== // Encryption Templates //======================================== ================================= private static string Makedigesttemplate (Hashalgorithm algo, string text) { byte[] data = algo.computehash (ASCIIEncoding.ASCII.GetBytes (text)); stringbuilder codes = new stringbuilder (); for (Int i = 0; i < data. LengTh; ++i) { codes. Append (Data[i]. ToString ("X2")); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP} return codes. ToString (); }  }}
The use method is simple, as follows: string strmd5 = Mymessagedigest.hexmd5 ("Oohacker"); String Strsha = Mymessagedigest.hexsha ("Oohacker"); Console.WriteLine ("Hex MD5 encryption: {0}", STRMD5); Console.WriteLine ("Hex SHA-1 encryption: {0}", Strsha);
The output results are as follows:
Hex MD5 encryption:eccca05fb779dcece81656890773b7bf Hex SHA-1 encryption:67576ffe5410fb70651ef8fd550ecdb7a9901603