A very simplified C # message digest algorithm __ algorithm

Source: Internet
Author: User
Tags md5 md5 encryption

  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 ();     }  &nbsp}}

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

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.