How to generate PHP-like MD5 Hash Code in C #

Source: Internet
Author: User
Tags md5 encryption md5 hash

How to generate PHP-like MD5 Hash Code in C #

Recently in an existing system for C # transformation, the system was previously used in PHP, the background of the administrator login with the MD5 encryption algorithm. In PHP, MD5 encryption of a string is very simple, one line of code:

MD5 ("Something want to encrypt.")

Call the MD5 () method directly and then pass in the string that will be MD5 encrypted, and you will get the hash code returned. There should also be a corresponding algorithm in C #! Is that right? I first tried the following code, resulting in the hash code and PHP is not the same.

 Public Static string MD5 (string  stringtohash) {    return"MD5");}

So, we have to borrow C # 's MD5CryptoServiceProvider object to write the code itself to convert.

1. Instantiating MD5CryptoServiceProvider objects

2. Converting a string into a byte array

3. Use the ComputeHash () method of the MD5CryptoServiceProvider object to encrypt the byte array, returning the converted byte array

4. Before you can convert a byte array into a string, you also need to traverse it and do the following conversions:

Mybyte.tostring ("x2"). ToLower ()

Then, you can get the same MD5 hash code as in PHP. Why the. NET to be so troublesome, perhaps this is why so many developers are still enthusiastic about PHP development of one of the reasons, each programming language has its own charm, but also has its meaning!

Based on the above discussion, the complete code is as follows:

 Public Static stringMd5forphp (stringStringtohash) {    varMD5 =NewSystem.Security.Cryptography.MD5CryptoServiceProvider (); byte[] Emailbytes =Encoding.UTF8.GetBytes (Stringtohash.tolower ()); byte[] Hashedemailbytes =Md5.computehash (emailbytes); StringBuilder SB=NewStringBuilder (); foreach(varBinchhashedemailbytes) {sb. Append (B.tostring ("X2").    ToLower ()); }    returnsb. ToString ();}

Alternatively, you can write the above method as a C # extension method, just modify the method signature.

 Public Static string md5forphp (This string  stringtohash) {    //  Your code here.}

PHP programs and C # programs involve conversion between formats in many ways, and if the server that is running PHP is of type UNIX, there is also a conversion between the date formats. The following two methods show how to convert Unix time to C # datetime and how to convert C # datetime to Unix time.

 Public StaticDateTime Unixtimestamptodatetime (LongUnixtimestamp) {    //Unix Timestamp is seconds past epochDateTime Dtdatetime =NewDateTime (1970,1,1,0,0,0,0, SYSTEM.DATETIMEKIND.UTC);
returndtdatetime.addseconds (Unixtimestamp);} Public Static Longdatetimetounixtimestamp (datetime datetime) {TimeSpan span= (DateTime-NewDateTime (1970,1,1,0,0,0,0, DATETIMEKIND.UTC)); return(Long) span. TotalSeconds;}

How to generate PHP-like MD5 Hash Code in C #

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.