Click here to Skip to main content
15,860,972 members
Articles / Web Development / ASP.NET
Article

An MD5 Function Compatible with PHP

Rate me:
Please Sign up or sign in to vote.
4.00/5 (12 votes)
14 Jul 2005 95K   14   18
Describes an implementation of a MD5 function compatible with the PHP function.

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:

C#
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


Written By
Web Developer
Chile Chile
Eduardo Diaz
personal blog

Comments and Discussions

 
GeneralMD5 Function Compatible with PHP Pin
JANAZA11-Sep-16 23:20
professionalJANAZA11-Sep-16 23:20 
QuestionLicense? Pin
Abi Bradbury18-Dec-14 22:00
Abi Bradbury18-Dec-14 22:00 
GeneralGreat! Translated it to VB.NET Pin
milesplit8-Mar-10 11:28
milesplit8-Mar-10 11:28 
GeneralRe: Great! Translated it to VB.NET Pin
maxfloden12-Apr-10 1:10
maxfloden12-Apr-10 1:10 
GeneralAppendFormat Pin
Jaime Olivares21-Jul-08 0:11
Jaime Olivares21-Jul-08 0:11 
GeneralThank you for this great tip... Pin
Gokhan Mamaci25-May-08 3:30
professionalGokhan Mamaci25-May-08 3:30 
GeneralsugarCRM Pin
pkaminiv26-Jul-07 21:01
pkaminiv26-Jul-07 21:01 
GeneralThank you!! Pin
Acestes24-Feb-07 1:56
Acestes24-Feb-07 1:56 
QuestionHad any luck with set_entry ? Pin
jpevans22-Feb-06 4:00
jpevans22-Feb-06 4:00 
Hi Eduardo -

I am using the SOAP interface from .Net also. Most things are working just fine with one exception.

I need to use the set_entry method to add extra items to the record after creating an Account - like address - phone - ans so on.

set_entry requires you to use the name_value object to get these items in. Now I have set this up and running in debug I can see that the my name_value object gets populated correctly - but the set_entry method will not accept it as a valid parameter - even though it requires it!!

Have you had any luck with set_entry?

Thanks
GeneralAny more SugarCRM Tips Pin
cnoevil21-Dec-05 9:40
cnoevil21-Dec-05 9:40 
GeneralVery Thanks Pin
ibrahimuludag23-Oct-05 3:42
ibrahimuludag23-Oct-05 3:42 
GeneralTechnically speaking... Pin
Livid14-Jul-05 6:33
Livid14-Jul-05 6:33 
GeneralRe: Technically speaking... Pin
ediazc14-Jul-05 6:43
ediazc14-Jul-05 6:43 
GeneralRe: Technically speaking... Pin
Almighty Bob14-Jul-05 7:52
Almighty Bob14-Jul-05 7:52 
GeneralRe: Technically speaking... Pin
ediazc14-Jul-05 8:12
ediazc14-Jul-05 8:12 
GeneralRe: Technically speaking... Pin
Russ Harding21-Oct-05 3:03
Russ Harding21-Oct-05 3:03 
GeneralRe: Technically speaking... Pin
ediazc21-Oct-05 3:25
ediazc21-Oct-05 3:25 
GeneralRe: Technically speaking... Pin
Wietse Kok26-Sep-05 10:16
Wietse Kok26-Sep-05 10:16 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.