This snippet function hashes a string into an MD5. The function will throw an error if an empty string is provided. Just copy and paste this into a console application, set the Sub Main() as the start up object and run it! You will see how the same hash is created for the "Test" string and a different hash is created for the "Test1" string. If you've got any problems with this snippet just comment or private message me.
Imports System.Security.Cryptography
Imports System.Text.Encoding
Class HashingClass
'Testing your application
Shared Sub Main()
Dim objectInstance As New HashingClass
Try
Console.WriteLine(objectInstance.GetStringMD5("Test"))
Console.WriteLine(objectInstance.GetStringMD5("Test"))
Console.WriteLine(objectInstance.GetStringMD5("Test1"))
Console.WriteLine(objectInstance.GetStringMD5(""))
Catch md5Exception As Exception
Console.WriteLine(String.Format("Error encountered: {0} - Thrown by: {1}", _
md5Exception.Message, _
md5Exception.TargetSite.Name))
Finally
Console.ReadLine()
End Try
End Sub
'The following function hashes a given string using MD5 ...
Function GetStringMD5(ByVal dataToHash As String) As String
If dataToHash = String.Empty Then
Throw New ApplicationException("Cannot hash empty string!")
Else
Dim hashMechanism As HashAlgorithm = HashAlgorithm.Create("MD5")
Dim data = [Default].GetBytes(dataToHash)
Dim finalHash = hashMechanism.ComputeHash(data)
Return [Default].GetString(finalHash)
End If
End Function
End Class