Friday, September 12, 2008

Create a MD5 Hash in VB.Net

Creating a compliant MD5 Hash was a tough find for VB... I looked all over the place and found people doing all kinds of thing for hashes... But I needed one that would be the same as a Perl/PHP hash... After a lot of searching, I mashed together (as most of us do) a couple of ideas about the cryproservices and string building to come up with this...


Imports System.Security.Cryptography

Public Function Md5Hasher(ByVal StringToHash As String) As String
Dim md5Crypter As MD5
md5Crypter = MD5CryptoServiceProvider.Create
Dim DataBytes() As Byte = md5Crypter.ComputeHash(Encoding.Default.GetBytes(StringToHash ))
Dim strbldr As StringBuilder = New StringBuilder
Dim i As Integer
For i = 0 To DataBytes.Length - 1
strbldr.AppendFormat("{0:x2}", dataMd5(i))
Next

Return strbldr .ToString

End Function

No comments: