Click here to Skip to main content
11,434,799 members (49,136 online)
Click here to Skip to main content

An MD5 Function Compatible with PHP

, 14 Jul 2005
Rate this:
Please Sign up or sign in to vote.
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:

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

Share

About the Author

ediazc
Web Developer
Chile Chile
Eduardo Diaz
personal blog

Comments and Discussions

 
QuestionLicense? Pin
Abi Bradbury18-Dec-14 23:00
memberAbi Bradbury18-Dec-14 23:00 
GeneralGreat! Translated it to VB.NET Pin
milesplit8-Mar-10 12:28
membermilesplit8-Mar-10 12:28 
GeneralRe: Great! Translated it to VB.NET Pin
maxfloden12-Apr-10 2:10
membermaxfloden12-Apr-10 2:10 
GeneralAppendFormat Pin
Jaime Olivares21-Jul-08 1:11
memberJaime Olivares21-Jul-08 1:11 
GeneralThank you for this great tip... Pin
Gokhan Mamaci25-May-08 4:30
memberGokhan Mamaci25-May-08 4:30 
GeneralsugarCRM Pin
pkaminiv26-Jul-07 22:01
memberpkaminiv26-Jul-07 22:01 
GeneralThank you!! Pin
Acestes24-Feb-07 2:56
memberAcestes24-Feb-07 2:56 
Thank you for posting your code. It's saved me a huge ammount of time as I can now integrate users from a PHPBB forum and website that I've taken over. Big Grin | :-D
QuestionHad any luck with set_entry ? Pin
jpevans22-Feb-06 5:00
memberjpevans22-Feb-06 5:00 
GeneralAny more SugarCRM Tips Pin
cnoevil21-Dec-05 10:40
membercnoevil21-Dec-05 10:40 
GeneralVery Thanks Pin
ibrahimuludag23-Oct-05 4:42
memberibrahimuludag23-Oct-05 4:42 
GeneralTechnically speaking... Pin
Livid14-Jul-05 7:33
memberLivid14-Jul-05 7:33 
GeneralRe: Technically speaking... Pin
ediazc14-Jul-05 7:43
memberediazc14-Jul-05 7:43 
GeneralRe: Technically speaking... Pin
<span style='color:#6699cc; '>MadHatter ¢</span>14-Jul-05 8:52
memberMadHatter ¢14-Jul-05 8:52 
GeneralRe: Technically speaking... Pin
ediazc14-Jul-05 9:12
memberediazc14-Jul-05 9:12 
GeneralRe: Technically speaking... Pin
RHarding_121-Oct-05 4:03
memberRHarding_121-Oct-05 4:03 
GeneralRe: Technically speaking... Pin
ediazc21-Oct-05 4:25
memberediazc21-Oct-05 4:25 
GeneralRe: Technically speaking... Pin
WietseKok26-Sep-05 11:16
memberWietseKok26-Sep-05 11:16 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150428.2 | Last Updated 14 Jul 2005
Article Copyright 2005 by ediazc
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid