Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Security > MD5 implementation

Reply
Thread Tools

MD5 implementation

 
 
Casper Hornstrup
Guest
Posts: n/a
 
      02-04-2004
I need to use MD5 to generate a hash of a string. It needs to be
compatible with the MD5 implementation in PHP.

public string ComputeMD5(string plain)
{
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
byte[] bMD5Hash = Encoding.ASCII.GetBytes(plain);
return Encoding.ASCII.GetString(md5.ComputeHash(bMD5Hash) );
//return Convert.ToBase64String(md5.ComputeHash(bMD5Hash));
}


With PHP I get:
md5('test1test1test1test1test1test1') = '0b2fc97f2a37500552d805c4727295a8'

In .NET I get:
ComputeMD5('test1test1test1test1test1test1') = ' /I*7PRXDrr('
ComputeMD5('test1test1test1test1test1test1') = 'Cy/Jfyo3UAVS2AXEcnKVqA=='
(base64)

Am I doing it wrong or are the PHP and .NET implementations not compatible?

Thanks in advance,
Casper Hornstrup



 
Reply With Quote
 
 
 
 
Hernan de Lahitte
Guest
Posts: n/a
 
      02-04-2004
Hi,

If you compare both hashes in the same encoding (the PHP version is in
Hexadecimal and the .NET version is in base64 or binary.), they will match.

--
Hernan de Lahitte
Lagash Systems S.A.
http://www.lagash.com



"Casper Hornstrup" <(E-Mail Removed)> wrote in message
news:OZ$(E-Mail Removed)...
> I need to use MD5 to generate a hash of a string. It needs to be
> compatible with the MD5 implementation in PHP.
>
> public string ComputeMD5(string plain)
> {
> MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
> byte[] bMD5Hash = Encoding.ASCII.GetBytes(plain);
> return Encoding.ASCII.GetString(md5.ComputeHash(bMD5Hash) );
> //return Convert.ToBase64String(md5.ComputeHash(bMD5Hash));
> }
>
>
> With PHP I get:
> md5('test1test1test1test1test1test1') = '0b2fc97f2a37500552d805c4727295a8'
>
> In .NET I get:
> ComputeMD5('test1test1test1test1test1test1') = ' /I*7PRXDrr('
> ComputeMD5('test1test1test1test1test1test1') = 'Cy/Jfyo3UAVS2AXEcnKVqA=='
> (base64)
>
> Am I doing it wrong or are the PHP and .NET implementations not

compatible?
>
> Thanks in advance,
> Casper Hornstrup
>
>
>



 
Reply With Quote
 
 
 
 
Casper Hornstrup
Guest
Posts: n/a
 
      02-04-2004
Thanks for you answer.
How do I get a string in the .NET version that I can compare to the PHP
generated string and have a match?

Casper

"Hernan de Lahitte" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi,
>
> If you compare both hashes in the same encoding (the PHP version is in
> Hexadecimal and the .NET version is in base64 or binary.), they will

match.
>
> --
> Hernan de Lahitte
> Lagash Systems S.A.
> http://www.lagash.com
>
>
>
> "Casper Hornstrup" <(E-Mail Removed)> wrote in message
> news:OZ$(E-Mail Removed)...
> > I need to use MD5 to generate a hash of a string. It needs to be
> > compatible with the MD5 implementation in PHP.
> >
> > public string ComputeMD5(string plain)
> > {
> > MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
> > byte[] bMD5Hash = Encoding.ASCII.GetBytes(plain);
> > return Encoding.ASCII.GetString(md5.ComputeHash(bMD5Hash) );
> > //return Convert.ToBase64String(md5.ComputeHash(bMD5Hash));
> > }
> >
> >
> > With PHP I get:
> > md5('test1test1test1test1test1test1') =

'0b2fc97f2a37500552d805c4727295a8'
> >
> > In .NET I get:
> > ComputeMD5('test1test1test1test1test1test1') = ' /I*7PRXDrr('
> > ComputeMD5('test1test1test1test1test1test1') =

'Cy/Jfyo3UAVS2AXEcnKVqA=='
> > (base64)
> >
> > Am I doing it wrong or are the PHP and .NET implementations not

> compatible?
> >
> > Thanks in advance,
> > Casper Hornstrup
> >
> >
> >

>
>



 
Reply With Quote
 
Hernan de Lahitte
Guest
Posts: n/a
 
      02-04-2004
This code will encode in Hexa your hashed byte array.

public static string EncodeHexString(byte[] sArray)
{
StringBuilder sb = new StringBuilder( sArray.Length * 2 );
for(int index = 0; index < sArray.Length; index++)
{
sb.AppendFormat( "{0:X2}", sArray[index] );
}
return sb.ToString();
}

--
Hernan de Lahitte
Lagash Systems S.A.
http://www.lagash.com



"Casper Hornstrup" <(E-Mail Removed)> wrote in message
news:#(E-Mail Removed)...
> Thanks for you answer.
> How do I get a string in the .NET version that I can compare to the PHP
> generated string and have a match?
>
> Casper
>
> "Hernan de Lahitte" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > Hi,
> >
> > If you compare both hashes in the same encoding (the PHP version is in
> > Hexadecimal and the .NET version is in base64 or binary.), they will

> match.
> >
> > --
> > Hernan de Lahitte
> > Lagash Systems S.A.
> > http://www.lagash.com
> >
> >
> >
> > "Casper Hornstrup" <(E-Mail Removed)> wrote in message
> > news:OZ$(E-Mail Removed)...
> > > I need to use MD5 to generate a hash of a string. It needs to be
> > > compatible with the MD5 implementation in PHP.
> > >
> > > public string ComputeMD5(string plain)
> > > {
> > > MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
> > > byte[] bMD5Hash = Encoding.ASCII.GetBytes(plain);
> > > return Encoding.ASCII.GetString(md5.ComputeHash(bMD5Hash) );
> > > //return Convert.ToBase64String(md5.ComputeHash(bMD5Hash));
> > > }
> > >
> > >
> > > With PHP I get:
> > > md5('test1test1test1test1test1test1') =

> '0b2fc97f2a37500552d805c4727295a8'
> > >
> > > In .NET I get:
> > > ComputeMD5('test1test1test1test1test1test1') = ' /I*7PRXDrr('
> > > ComputeMD5('test1test1test1test1test1test1') =

> 'Cy/Jfyo3UAVS2AXEcnKVqA=='
> > > (base64)
> > >
> > > Am I doing it wrong or are the PHP and .NET implementations not

> > compatible?
> > >
> > > Thanks in advance,
> > > Casper Hornstrup
> > >
> > >
> > >

> >
> >

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Knowing the implementation, are all undefined behaviours become implementation-defined behaviours? Michael Tsang C++ 32 03-01-2010 09:15 PM
create a md5 / md5 passwd with a salt Peter Woodsky Ruby 6 11-21-2008 09:08 AM
md5 from python different then md5 from command line ursache.marius@gmail.com Python 9 05-07-2006 11:49 PM
MD5 implementation help please... Aerodyne Java 2 06-30-2005 06:26 AM
I remember someone asking about an MD5 javascript: http://pajhome.org.uk/crypt/md5/ Mozzie \( v \) Javascript 0 07-12-2004 01:06 PM



Advertisments