Skip Navigation

MD5 hashing in .NET to match PHP

16 February 2006 10:39 by steve.temple

I had a bit of a struggle recently trying to get .NET to hash a string that would match PHPs built in MD5 hashing. Ended up that it was a string encoding issue, heres how I got it to work:

'--------------------------------------------------------------------
' Hash the string to match PHPs MD5
'--------------------------------------------------------------------

Private Function Hash(ByVal sValue As String) As String

Dim oTextBytes As Byte() =
System.Text.Encoding.ASCII.GetBytes(sValue)

Dim cryptHandler As
System.Security.Cryptography.MD5CryptoServiceProvider

cryptHandler = New System.Security.Cryptography.MD5CryptoServiceProvider

Dim oHash As Byte() = cryptHandler.ComputeHash(oTextBytes)

Return LCase(EncodeHexString(oHash))

End Function

'--------------------------------------------------------------------
' Encode the hex string to match PHP MD5
'--------------------------------------------------------------------
Public Function EncodeHexString(ByVal sArray As Byte()) as String

Dim sb As System.text.StringBuilder = New
System.text.StringBuilder(sArray.Length * 2)

Dim index As Integer

For index = 0 To sArray.Length - 1

sb.AppendFormat("{0:X2}",
sArray(index))

Next

Return sb.ToString()
End Function

ConfigurationSettings.AppSettings is obsolete

13 December 2005 17:19 by steve.temple

I've been porting some code from .NET 1.1 to .NET 2. The compiler gives me a few of the following error:

'System.Configuration.ConfigurationSettings.AppSettings' is obsolete: 'This method is obsolete, it has been replaced by ConfigurationManager.AppSettings'

It seems you can no longer do this:

System.Configuration.ConfigurationSettings.AppSettings[connstr].ToString();

You should do this instead:

System.Configuration.ConfigurationManager.AppSettings[connstr].ToString();

you then get an error where ConfigurationManager can't be found, apparently the System.Configuration.ConfigurationSettings class is found in the System.dll assembly. The ConfigurationManager class is found in the system System.Configuration.dll which must be referenced first.