-
May 5th, 2012, 05:26 AM
#1
Thread Starter
Fanatic Member
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:
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
-
May 7th, 2012, 03:57 PM
#2
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:
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
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.
-
May 8th, 2012, 11:59 AM
#3
Frenzied Member
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.
-
May 8th, 2012, 03:33 PM
#4
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.
-
May 9th, 2012, 06:47 AM
#5
Thread Starter
Fanatic Member
Re: Get MD5 Hash From String
Thaks MattP I Them functions be handy.
-
May 6th, 2014, 07:32 PM
#6
Registered User
Re: Get MD5 Hash From String
Originally Posted by 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.
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
-
May 12th, 2014, 04:30 PM
#7
Re: Get MD5 Hash From String
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|