MD5 implementation

  • Thread starter Casper Hornstrup
  • Start date
C

Casper Hornstrup

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
 
H

Hernan de Lahitte

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

Casper Hornstrup

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 said:
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 said:
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
 
H

Hernan de Lahitte

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 said:
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 said:
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 said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top