PHP MD5 Hash and .net MD5 not matching


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:

md5(md5($var1). $var2)


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)


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)


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

        Return strOutput

    End Function

Open in new window

Who is Participating?
doctor069Author Commented:
Thanks -

No matter what I do I always get...

VB Hash is 5750b2229f7d00f955b235616f633fdc

I think the issue is in the "double" hashing

md5(md5($var1). $var2)

From my asp .net eyes this looks like:
$var1 get hashed first
then we append $var2
and hash again

But I suspect that that is not the case in PHP

I don't know much about php either, but one obvious thing that stands out is that it you are using utf-8 encoding and then getting the hex code for each byte..  I don't know if php uses utf-8 and I don't know if you want to be converting it to hex.  PHP might be encoding it as a base64 string, which sounds reasonable given its general web development use.  Also, I don't know if the "dot" is appending the string in php, so my appologies about not knowing for sure what I'm talking about.

Here is what my hash function looks like:
Public Function GenerateHash(ByVal aString As String, ByVal encoding As Text.Encoding, ByVal hashAlgorithm As HashAlgorithm) As String
    If encoding Is Nothing Then encoding = New Text.UTF8Encoding()
    If hashAlgorithm Is Nothing Then hashAlgorithm = New MD5CryptoServiceProvider()
    Dim byteSourceText() As Byte = encoding.GetBytes(aString)
    Dim hashBytes() As Byte = hashAlgorith.ComputeHash(byteSourceText)
    Return Convert.ToBase64String(hashBytes)
End Function

Open in new window

I don't know if that helps you, but it might be worth a shot ;) Obviously you don't need to take in the optional encoding and hashAlgorithm arguments, that just a straight copy and paste from my personal encryption library..

Instead of base64 encoding it you might try the literal string value of the bytes (again, you may want to check the encoding).
Return encoding.GetString(hashBytes)

Open in new window

best of luck.
doctor069Author Commented:
Going to close this and try it in the PHP group
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.