Link to home
Start Free TrialLog in
Avatar of doctor069
doctor069Flag for Canada

asked on

PHP MD5 Hash and .net MD5 not matching

Hi-

I have to get data from an external PHP site to my .net site.

The developer uses the following PHP code to get a hash:

$var1=1318437972;
$var2=01CdzTgUNgxIg;
md5(md5($var1). $var2)

Hash=06b27a58c07faee223d69ca9e4a87163

I am trying to get the same hash in vb .net but the hashes are different:

  Dim var1 As String = "1318437972"
  Dim var2 As String = "01CdzTgUNgxIg"
  Dim MD5Time As String = MD5(var1) & var2
  Dim MD5Out As String = MD5(MD5Time)

MD5Out=5750b2229f7d00f955b235616f633fdc

So...
PHP Hash is 06b27a58c07faee223d69ca9e4a87163
VB Hash is 5750b2229f7d00f955b235616f633fdc

PS  - My MD5 Function is provided below

Thanks in advance

Private Function MD5(ByVal str As String) As String

        Dim provider As MD5CryptoServiceProvider
        Dim bytValue() As Byte
        Dim bytHash() As Byte
        Dim strOutput As String = ""
        Dim i As Integer

        provider = New MD5CryptoServiceProvider()

        bytValue = System.Text.Encoding.UTF8.GetBytes(str)

        bytHash = provider.ComputeHash(bytValue)

        provider.Clear()

        For i = 0 To bytHash.Length - 1
            strOutput &= bytHash(i).ToString("x").PadLeft(2, "0")
        Next

        Return strOutput

    End Function

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of doctor069

ASKER

Hi-

The does shed some light. Maybe the hash code is messed up on the other end!
kaufmed did you use:

$var1=1318437972;
$var2=01CdzTgUNgxIg;
md5(md5($var1). $var2)
This is my PHP test:
<?php
	$var1="1318437972";
	$var2="01CdzTgUNgxIg";

	print_r(md5(md5($var1). $var2));

?>

Open in new window

The PHP in the original post will throw a parse error because the $var2 value is a string, but it is not quoted.  I tried it like this code snippet.

http://www.laprbass.com/RAY_temp_doctor069.php
outputs
5750b2229f7d00f955b235616f633fdc
<?php // RAY_temp_doctor069.php
error_reporting(E_ALL);
echo "<pre>";

// FROM THE POST AT EE, MODIFIED TO USE A STRING VARIABLE FOR $var2
$var1 = 1318437972;
$var2 = '01CdzTgUNgxIg';
$thing = md5(md5($var1). $var2);

echo $thing;

Open in new window

Turns out the programmer on the other end had a typo in his string and my code was good after all!

Thanks for you help!