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




Reply With Quote