rsrobbins
rsrobbins

Reputation: 639

PHP MD5 Not Matching VB.NET MD5

I am trying to get the same MD5 hash in PHP and VB.NET but no matter what text encoding I use I cannot get the hashes to match. What am I doing wrong? Below is my code in each language:

PHP Code:

echo '<b>$20.00 (UTF-32)</b> = ' . md5(mb_convert_encoding('1C9BRPS3TN85I2ULE5FP' + '20.00', 'UTF-32'));  
echo '<b>$20.00 (UTF-32LE)</b> = ' . md5(mb_convert_encoding('1C9BRPS3TN85I2ULE5FP' + '20.00', 'UTF-32LE'));
echo '<b>$20.00 (UTF-32BE)</b> = ' . md5(mb_convert_encoding('1C9BRPS3TN85I2ULE5FP' + '20.00', 'UTF-32BE'));
echo '<b>$20.00 (UTF-8)</b> = ' . md5(mb_convert_encoding('1C9BRPS3TN85I2ULE5FP' + '20.00', 'UTF-8'));
echo '<b>$20.00 (UTF-7)</b> = ' . md5(mb_convert_encoding('1C9BRPS3TN85I2ULE5FP' + '20.00', 'UTF-7'));
echo '<b>$20.00 (UTF-16)</b> = ' . md5(mb_convert_encoding('1C9BRPS3TN85I2ULE5FP' + '20.00', 'UTF-16'));
echo '<b>$20.00 (UTF-16LE)</b> = ' . md5(mb_convert_encoding('1C9BRPS3TN85I2ULE5FP' + '20.00', 'UTF-16LE'));
echo '<b>$20.00 (UTF-16BE)</b> = ' . md5(mb_convert_encoding('1C9BRPS3TN85I2ULE5FP' + '20.00', 'UTF-16BE'));
echo '<b>$20.00 (ASCII)</b> = ' . md5(mb_convert_encoding('1C9BRPS3TN85I2ULE5FP' + '20.00', 'ASCII'));

VB.NET Code:

Try
' Create a new instance of the MD5 object.
Dim md5Hasher As MD5 = MD5.Create()
' Convert the input string to a byte array and compute the hash.
Dim strInput As String = "1C9BRPS3TN85I2ULE5FP" & "20.00"
Dim data As Byte() = md5Hasher.ComputeHash(Encoding.UTF32.GetBytes(strInput))

' Create a new Stringbuilder to collect the bytes
' and create a string.
Dim sBuilder As New StringBuilder()

' Loop through each byte of the hashed data 
' and format each one as a hexadecimal string.
Dim i As Integer
For i = 0 To data.Length - 1
    sBuilder.AppendFormat("{0:x2}", data(i))
    'sBuilder.Append(data(i).ToString("x2"))
Next i

lblResponse.Text = "<b>$20.00 (UTF-32)</b> = " & sBuilder.ToString() & "<br>"

' Try a different encoding
data = md5Hasher.ComputeHash(Encoding.ASCII.GetBytes(strInput))
' Loop through each byte of the hashed data 
' and format each one as a hexadecimal string.
sBuilder.Length = 0
For i = 0 To data.Length - 1
    sBuilder.AppendFormat("{0:x2}", data(i))
Next i
lblResponse.Text &= "<b>$20.00 (ASCII)</b> = " & sBuilder.ToString() & "<br>"

' Try a different encoding
data = md5Hasher.ComputeHash(Encoding.Unicode.GetBytes(strInput))
' Loop through each byte of the hashed data 
' and format each one as a hexadecimal string.
sBuilder.Length = 0
For i = 0 To data.Length - 1
    sBuilder.AppendFormat("{0:x2}", data(i))
Next i
lblResponse.Text &= "<b>$20.00 (Unicode)</b> = " & sBuilder.ToString() & "<br>"

' Try a different encoding
data = md5Hasher.ComputeHash(Encoding.UTF7.GetBytes(strInput))
' Loop through each byte of the hashed data 
' and format each one as a hexadecimal string.
sBuilder.Length = 0
For i = 0 To data.Length - 1
    sBuilder.AppendFormat("{0:x2}", data(i))
Next i
lblResponse.Text &= "<b>$20.00 (UTF7)</b> = " & sBuilder.ToString() & "<br>"

' Try a different encoding
data = md5Hasher.ComputeHash(Encoding.UTF8.GetBytes(strInput))
' Loop through each byte of the hashed data 
' and format each one as a hexadecimal string.
sBuilder.Length = 0
For i = 0 To data.Length - 1
    sBuilder.AppendFormat("{0:x2}", data(i))
Next i
lblResponse.Text &= "<b>$20.00 (UTF8)</b> = " & sBuilder.ToString() & "<br>"

Catch ex As Exception
    lblErrorMessage.Text = ex.ToString()
End Try

Upvotes: 0

Views: 847

Answers (1)

MrTux
MrTux

Reputation: 34003

String concatenation is . in PHP not + as in VB.Net. Thus, '1C9BRPS3TN85I2ULE5FP' + '20.00' won't result in '1C9BRPS3TN85I2ULE5FP20.00'in PHP.

Edit: VB.net's Real string concatenation is & but since the term visual basic is used, + sign can do the same as long as mathematical interpretation is meaningless. So to be on the safe side, use &.

Upvotes: 1

Related Questions