MD5 Hashing

sampoo

Active member
Joined
Jun 12, 2004
Messages
25
Location
Belgium
Programming Experience
3-5
Hi, I have to convert a string to an MD5 hash...

I know that there is a class available, as you can read beneath:

VB.NET:
  	Public Function GenerateHash(ByVal SourceText As String) As String
  		'Create an encoding object to ensure the encoding standard for the source text
  		Dim Ue As New UnicodeEncoding
  		'Retrieve a byte array based on the source text
  		Dim ByteSourceText() As Byte = Ue.GetBytes(SourceText)
  		'Instantiate an MD5 Provider object
  		Dim Md5 As New MD5CryptoServiceProvider
  		'Compute the hash value from the source
  		Dim ByteHash() As Byte = Md5.ComputeHash(ByteSourceText)
  
  		Return ByteHash.ToString
  	End Function

But, this code returns something that has maybe the same length of an MD5 hash, but surely does not look like one (containing e.g. these characters: =,T, O, Q, etc.). This is not possible for an MD5 since MD5 is always in hexadecimal characters...


How solve this?


Thanks
 
Try This,

Private Sub HashTextMD5(ByVal TextToHash As String)
Dim md5 As MD5CryptoServiceProvider
Dim bytValue() As Byte
Dim bytHash() As Byte
' Create New Crypto Service Provider Object
md5 = New MD5CryptoServiceProvider
' Convert the original string to array of Bytes
bytValue = System.Text.Encoding. _
UTF8.GetBytes(TextToHash)
' Compute the Hash, returns an array of Bytes
bytHash = md5.ComputeHash(bytValue)
md5.Clear()
' Return a base 64 encoded string of the Hash value
Debug.WriteLine(Convert.ToBase64String(bytHash))
End Sub

Mykre
 
I transformed your Sub a bit so that it could be used as a function, but it still gives only 24 characters (while an MD5 should be 32) and still with the weirdo signs in it....


Thanks
 
Back
Top