Click here to Skip to main content
Click here to Skip to main content

An MD5 Function Compatible with PHP

By , 14 Jul 2005
 

Introduction

Recently I needed to integrate an ASP.NET application with SugarCRM, an Open Source CRM implementation (a very good one).

SugarCRM publishes a SOAP API, but I needed to send the password in MD5 format compatible with the MD5 function of PHP (SugarCRM is built on PHP). So I wrote a function that emulates the MD5 function of PHP.

MD5 Function

In PHP the MD5 function receives a string and returns a hexadecimal number of length 32.

So the implementation of this function in C# is like this:

using System.Security.Cryptography;
using System.Text;

public sealed class PhpCompatible
{
   public static string Md5Hash (string pass)
   {
     MD5 md5 = MD5CryptoServiceProvider.Create ();
     byte[] dataMd5 = md5.ComputeHash (Encoding.Default.GetBytes (pass));
     StringBuilder sb = new StringBuilder();
     for (int i = 0; i < dataMd5.Length; i++)
       sb.AppendFormat("{0:x2}", dataMd5[i]);
     return sb.ToString ();
}

One difficulty I found is with the AppendFormat, because I needed to pad the hexadecimal digit with 0. The solution was to put a 2 after the x of the format string. In general the documentation of the Format functions is hard to understand.

There are a lot of PHP applications out there, and we need to have a mechanism to interact with them. I hope this function helps you if you are in a situation like the one I described.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

ediazc
Web Developer
Chile Chile
Member
Eduardo Diaz
personal blog

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralRe: Technically speaking...memberMadHatter ¢14 Jul '05 - 7:52 
there's some obscure function that hidden away somewhere in the web side of the framework. I personally wont use it (name is too long 1st off, and its implementation is obscure) and I think it should have shown up in the standard MD5 api. I typically do the same as you've shown when I need it.
 
System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile( ... )
 
is the one already included.
 



/bb|[^b]{2}/
 

GeneralRe: Technically speaking...memberediazc14 Jul '05 - 8:12 
It's a very obscure function! Dead | X|
If you want to use it in winforms need to include a reference to system.web!
Also i have to test if do the same formating on output.
Thanks, great tip.Smile | :)
 
Eduardo Diaz

Dark Side Programming Blog

Programando.Org
GeneralRe: Technically speaking...memberRHarding_121 Oct '05 - 3:03 
SCHWEET! You saved my life. I just started a new position and all of the MD5 hashing they are doing is using PHP. I would have climbed up the wall trying to figure this one out on my own.
GeneralRe: Technically speaking...memberediazc21 Oct '05 - 3:25 
I'm glad this article helped you.
However, MD5 has limitations, and you must be aware of some breakings on the algorithm.
Still can be used in certains contexts.
 
Eduardo Diaz
 
site | english blog | spanish blog
GeneralRe: Technically speaking...memberWietseKok26 Sep '05 - 10:16 
Thanks!!! This function solved my problems. The MSDN library is very bad!
 
Regards,
 
Wietse
The Netherlands
 
Regards,
 
Wietse
The Netherlands

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130523.1 | Last Updated 14 Jul 2005
Article Copyright 2005 by ediazc
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid