Results 1 to 7 of 7

Thread: Get MD5 Hash From String

  1. #1

    Thread Starter
    Fanatic Member BenJones's Avatar
    Join Date
    Mar 2010
    Location
    Wales UK
    Posts
    629

    Get MD5 Hash From String

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

    Class code:

    vbnet Code:
    1. Imports System.Text
    2. Imports System.Security.Cryptography
    3.  
    4. Public Class cMd5Hash
    5.  
    6.     Public Function Md5FromString(ByVal Source As String) As String
    7.         Dim Bytes() As Byte
    8.         Dim sb As New StringBuilder()
    9.  
    10.         'Check for empty string.
    11.         If String.IsNullOrEmpty(Source) Then
    12.             Throw New ArgumentNullException
    13.         End If
    14.  
    15.         'Get bytes from string.
    16.         Bytes = Encoding.Default.GetBytes(Source)
    17.  
    18.         'Get md5 hash
    19.         Bytes = MD5.Create().ComputeHash(Bytes)
    20.  
    21.         'Loop though the byte array and convert each byte to hex.
    22.         For x As Integer = 0 To Bytes.Length - 1
    23.             sb.Append(Bytes(x).ToString("x2"))
    24.         Next
    25.  
    26.         'Return md5 hash.
    27.         Return sb.ToString()
    28.  
    29.     End Function
    30.  
    31. End Class

    Example code:

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

  2. #2
    Frenzied Member MattP's Avatar
    Join Date
    Dec 2008
    Location
    WY
    Posts
    1,227

    Re: Get MD5 Hash From String

    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:
    1. Imports System.Text
    2. Imports System.Security.Cryptography
    3.  
    4. Public Class MD5Class
    5.  
    6.     Private Shared ReadOnly _md5 As MD5 = MD5.Create()
    7.  
    8.     Public Function GetMd5Hash(source As String) As String
    9.  
    10.         Dim data = _md5.ComputeHash(Encoding.UTF8.GetBytes(source))
    11.         Dim sb As New StringBuilder()
    12.         Array.ForEach(data, Function(x) sb.Append(x.ToString("X2")))
    13.         Return sb.ToString()
    14.  
    15.     End Function
    16.  
    17.     Public Function VerifyMd5Hash(source As String, hash As String) As Boolean
    18.  
    19.         Dim sourceHash = GetMd5Hash(source)
    20.         Dim comparer As StringComparer = StringComparer.OrdinalIgnoreCase
    21.         Return If(comparer.Compare(sourceHash, hash) = 0, True, False)
    22.  
    23.     End Function
    24.  
    25.     Public Function GetMd5HashBase64(source As String) As String
    26.  
    27.         Dim data = _md5.ComputeHash(Encoding.UTF8.GetBytes(source))
    28.         Return Convert.ToBase64String(data)
    29.  
    30.     End Function
    31.  
    32.     Public Function VerifyMd5HashBase64(source As String, hash As String) As Boolean
    33.  
    34.         Dim sourceHash = GetMd5HashBase64(source)
    35.         Dim comparer As StringComparer = StringComparer.OrdinalIgnoreCase
    36.         Return If(comparer.Compare(sourceHash, hash) = 0, True, False)
    37.  
    38.     End Function
    39.  
    40. End Class

    Example uses:

    vb.net Code:
    1. Public Class Form1
    2.  
    3.     Private Const const1 As String = "Peter Piper picked a peck of pickled peppers."
    4.     Private Const const2 As String = "In 1493 Columbus sailed across the sea."
    5.  
    6.     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    7.  
    8.         Dim hashClass As New MD5Class()
    9.  
    10.         Dim hash1 As String = hashClass.GetMd5Hash(const1)
    11.  
    12.         Dim hash2 As String = hashClass.GetMd5Hash(const2)
    13.  
    14.         Display(hashClass, const1, hash1)
    15.  
    16.         Display(hashClass, const2, hash1)
    17.  
    18.         Dim hash3 As String = hashClass.GetMd5HashBase64(const1)
    19.  
    20.         Dim hash4 As String = hashClass.GetMd5HashBase64(const2)
    21.  
    22.         DisplayBase64(hashClass, const1, hash3)
    23.  
    24.         DisplayBase64(hashClass, const1, hash4)
    25.  
    26.     End Sub
    27.  
    28.     Private Shared Sub Display(hashClass As MD5Class, sourceString As String, hashString As String)
    29.  
    30.         MessageBox.Show(If(hashClass.VerifyMd5Hash(sourceString, hashString), "Hashes are the same", "Hashes are different"))
    31.  
    32.     End Sub
    33.  
    34.     Private Shared Sub DisplayBase64(hashClass As MD5Class, sourceString As String, hashString As String)
    35.  
    36.         MessageBox.Show(If(hashClass.VerifyMd5HashBase64(sourceString, hashString), "Hashes are the same", "Hashes are different"))
    37.  
    38.     End Sub
    39.  
    40. End Class
    This pattern in common to all great programmers I know: they're not experts in something as much as experts in becoming experts in something.

    The best programming advice I ever got was to spend my entire career becoming educable. And I suggest you do the same.

  3. #3
    Frenzied Member
    Join Date
    Jul 2006
    Location
    MI
    Posts
    1,965

    Re: Get MD5 Hash From String

    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.

  4. #4
    Frenzied Member MattP's Avatar
    Join Date
    Dec 2008
    Location
    WY
    Posts
    1,227

    Re: Get MD5 Hash From String

    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.
    This pattern in common to all great programmers I know: they're not experts in something as much as experts in becoming experts in something.

    The best programming advice I ever got was to spend my entire career becoming educable. And I suggest you do the same.

  5. #5

    Thread Starter
    Fanatic Member BenJones's Avatar
    Join Date
    Mar 2010
    Location
    Wales UK
    Posts
    629

    Re: Get MD5 Hash From String

    Thaks MattP I Them functions be handy.

  6. #6
    Registered User
    Join Date
    May 2014
    Posts
    1

    Re: Get MD5 Hash From String

    Quote Originally Posted by MattP View Post
    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

  7. #7

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width