You may want to put a function in there that validates hashes as well.
Here are a couple of examples that show how you could implement it.
vb.net Code:
Imports System.Text
Imports System.Security.Cryptography
Public Class MD5Class
Private Shared ReadOnly _md5 As MD5 = MD5.Create()
Public Function GetMd5Hash(source As String) As String
Dim data = _md5.ComputeHash(Encoding.UTF8.GetBytes(source))
Dim sb As New StringBuilder()
Array.ForEach(data, Function(x) sb.Append(x.ToString("X2")))
Return sb.ToString()
End Function
Public Function VerifyMd5Hash(source As String, hash As String) As Boolean
Dim sourceHash = GetMd5Hash(source)
Dim comparer As StringComparer = StringComparer.OrdinalIgnoreCase
Return If(comparer.Compare(sourceHash, hash) = 0, True, False)
End Function
Public Function GetMd5HashBase64(source As String) As String
Dim data = _md5.ComputeHash(Encoding.UTF8.GetBytes(source))
Return Convert.ToBase64String(data)
End Function
Public Function VerifyMd5HashBase64(source As String, hash As String) As Boolean
Dim sourceHash = GetMd5HashBase64(source)
Dim comparer As StringComparer = StringComparer.OrdinalIgnoreCase
Return If(comparer.Compare(sourceHash, hash) = 0, True, False)
End Function
End Class
Example uses:
vb.net Code:
Public Class Form1
Private Const const1 As String = "Peter Piper picked a peck of pickled peppers."
Private Const const2 As String = "In 1493 Columbus sailed across the sea."
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim hashClass As New MD5Class()
Dim hash1 As String = hashClass.GetMd5Hash(const1)
Dim hash2 As String = hashClass.GetMd5Hash(const2)
Display(hashClass, const1, hash1)
Display(hashClass, const2, hash1)
Dim hash3 As String = hashClass.GetMd5HashBase64(const1)
Dim hash4 As String = hashClass.GetMd5HashBase64(const2)
DisplayBase64(hashClass, const1, hash3)
DisplayBase64(hashClass, const1, hash4)
End Sub
Private Shared Sub Display(hashClass As MD5Class, sourceString As String, hashString As String)
MessageBox.Show(If(hashClass.VerifyMd5Hash(sourceString, hashString), "Hashes are the same", "Hashes are different"))
End Sub
Private Shared Sub DisplayBase64(hashClass As MD5Class, sourceString As String, hashString As String)
MessageBox.Show(If(hashClass.VerifyMd5HashBase64(sourceString, hashString), "Hashes are the same", "Hashes are different"))
End Sub
End Class