# VBForums CodeBank > CodeBank - Visual Basic .NET >  Get MD5 Hash From String

## BenJones

Hi little example of returning a hd5 hash from a string.
Hope it comes in useful comments suggestions welcome.

*Class code:*


vbnet Code:
Imports System.Text
Imports System.Security.Cryptography
 Public Class cMd5Hash
     Public Function Md5FromString(ByVal Source As String) As String
        Dim Bytes() As Byte
        Dim sb As New StringBuilder()
         'Check for empty string.
        If String.IsNullOrEmpty(Source) Then
            Throw New ArgumentNullException
        End If
         'Get bytes from string.
        Bytes = Encoding.Default.GetBytes(Source)
         'Get md5 hash
        Bytes = MD5.Create().ComputeHash(Bytes)
         'Loop though the byte array and convert each byte to hex.
        For x As Integer = 0 To Bytes.Length - 1
            sb.Append(Bytes(x).ToString("x2"))
        Next
         'Return md5 hash.
        Return sb.ToString()
     End Function
 End Class

*Example code:*


vbnet Code:
Private Sub cmdGetHash_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdGetHash.Click
        Dim HashCode As cMd5Hash
         HashCode = New cMd5Hash()
         MessageBox.Show(HashCode.Md5FromString("password"), "Md5", MessageBoxButtons.OK, MessageBoxIcon.Information)
     End Sub

----------


## MattP

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.TextImports 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

----------


## nbrege

You might want to explain what a hash is & why someone would want to use one, for those that don't know ... like me for example.  Just a suggestion.  :Wink:

----------


## MattP

MD5 or Message Digest Algorithm 5 is a cryptographic hash function used to check data integrity via a checksum.  There are several issues with it and US-CERT has stated that it "should be considered cryptographically broken and unsuitable for further use."

If you're looking to cryptography I would suggest you take a look at SHA-2.

----------


## BenJones

Thaks MattP I Them functions be handy.

----------


## bburgess

> MD5 or Message Digest Algorithm 5 is a cryptographic hash function used to check data integrity via a checksum.  There are several issues with it and US-CERT has stated that it "should be considered cryptographically broken and unsuitable for further use."
> 
> If you're looking to cryptography I would suggest you take a look at SHA-2.


I cant find anything (even Google) regarding SHA-2.   But I do find things like: SHA-1, SHA-256, SHA-384, SHA-512, MD4, MD5, RIPEMD.   Help?

Thx

----------


## ident

Help? the top google search is http://en.wikipedia.org/wiki/SHA-2

----------

