![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
Programmer
Join Date: Jul 2004
Location: Hampshire
Posts: 56
Rep Power: 5
![]() |
Md5
Hello.
I would like to MD5 a value of two text box's. txtOne txtTwo So I would like to make the two textboxes into a variable. Dim Text As String String = txtOne + txtTwo Then MD5 the variable. I am not sure how I do this but I have found this, which to me, appears to be relative. Function getMd5Hash(ByVal input As String) As String
' Create a new instance of the MD5 object.
Dim md5Hasher As MD5 = MD5.Create()
' Convert the input string to a byte array and compute the hash.
Dim data As Byte() = md5Hasher.ComputeHash(Encoding.Default.GetBytes(input))
' Create a new Stringbuilder to collect the bytes
' and create a string.
Dim sBuilder As New StringBuilder()
' Loop through each byte of the hashed data
' and format each one as a hexadecimal string.
Dim i As Integer
For i = 0 To data.Length - 1
sBuilder.Append(data(i).ToString("x2"))
Next i
' Return the hexadecimal string.
Return sBuilder.ToString()
End FunctionPlease can someone help me to make this work how I would like it to work ![]() |
|
|
|
|
|
#2 |
|
Expert Programmer
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4
![]() |
Please tell us what doesn't work, or what works wrong. We are quite good at programming, but we fail miserably at mind reading.
|
|
|
|
|
|
#3 | |
|
Programmer
Join Date: Jul 2004
Location: Hampshire
Posts: 56
Rep Power: 5
![]() |
Quote:
The second bit of code is something I have found on the internet concerning strings and MD5. I am tring to convert this to something that will change a value of a variable (string variable) to MD5, but I am unsure as to how I would do this. I got the code from here: http://msdn2.microsoft.com/en-us/lib...aphy.md5.aspx# Last edited by emdiesse; Jan 6th, 2006 at 6:52 PM. |
|
|
|
|
|
|
#4 |
|
Expert Programmer
|
OK, just a few things about that code: Firstly the data should be passed as a reference type to prevent needless duplication of memory, which is important if you're hashing a relatively large amount of data.
Secondly, however, if you're using this to hash entire files, you should use System.Security.Cryptography.MD5.ComputeHash with a filestream instead, and wrapper this in a callback thread so that your application does not "freeze" while the hash is being calculated, perhaps even giving user feedback. Then again if the reliability and speed are important, you might want to look for an open source optimised ASM library like OpenSSL instead, as the .NET FW's routines are written in CLR and are appallingly slow. Anyway here's your function: Public Function getMd5Hash(ByRef Data As String) As String
Dim md5Hasher As System.Security.Cryptography.MD5 = System.Security.Cryptography.MD5.Create()
Dim Hash As Byte() = md5Hasher.ComputeHash(System.Text.Encoding.ASCII.GetBytes(Data))
Dim sBuilder As System.Text.StringBuilder = New System.Text.StringBuilder(32)
For I As Integer = 0 To Hash.Length - 1
sBuilder.Append(Hash(I).ToString("x2"))
Next I
Return sBuilder.ToString()
End Function |
|
|
|
|
|
#5 | |
|
Programmer
Join Date: Jul 2004
Location: Hampshire
Posts: 56
Rep Power: 5
![]() |
Quote:
![]() I have made the amendments to my code and I have this as the code: Imports System
Imports System.Security.Cryptography
Imports System.Text
Module Generate
' Hash an input string and return the hash as
' a 32 character hexadecimal string.
Public Function getMd5Hash(ByRef Data As String) As String
Dim md5Hasher As System.Security.Cryptography.MD5 = System.Security.Cryptography.MD5.Create()
Dim Hash As Byte() = md5Hasher.ComputeHash(System.Text.Encoding.ASCII.GetBytes(Data))
Dim sBuilder As System.Text.StringBuilder = New System.Text.StringBuilder(32)
For I As Integer = 0 To Hash.Length - 1
sBuilder.Append(Hash(I).ToString("x2"))
Next I
Return sBuilder.ToString()
End Function
Sub Main()
Dim source As String
Dim frmMd5 As New frmMd5
source = frmMd5.txtOne.Text & frmMd5.txtApp.Text
Dim hash As String = getMd5Hash(source)
frmMd5.txtEncryptedText.Text = hash
End Sub
End Module
Public Class frmMd5
Inherits System.Windows.Forms.Form
Private Sub btnGen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGen.Click
Main() 'Run Sub Main(), Generates md5I get this error when I click generate: An unhandled exception of type 'System.InvalidOperationException' occurred in system.windows.forms.dll Additional information: It is invalid to start a second message loop on a single thread. Use Application.RunDialog or Form.ShowDialog instead. Please help ![]() |
|
|
|
|
|
|
#6 |
|
Programmer
Join Date: Jul 2004
Location: Hampshire
Posts: 56
Rep Power: 5
![]() |
Ahah. Done it.
I would now like to know how to change the length of the the output of my Md5 hash. I would like it to be possible to make it longer or shorter by the use of another text box. How do I do this? Do the numbers have to be base 2 (2,4,8,16,32,64,etc), or can they be any integer? I have changed the code to this: Imports System
Imports System.Security.Cryptography
Imports System.Text
Module Generate
Public Function getMd5Hash(ByRef Data As String) As String
Dim md5Hasher As System.Security.Cryptography.MD5 = System.Security.Cryptography.MD5.Create()
Dim Hash As Byte() = md5Hasher.ComputeHash(System.Text.Encoding.ASCII.GetBytes(Data))
Dim sBuilder As System.Text.StringBuilder = New System.Text.StringBuilder(32)
For I As Integer = 0 To Hash.Length - 1
sBuilder.Append(Hash(I).ToString("x2"))
Next I
Return sBuilder.ToString()
End Function
End Module
Public Class frmMd5
Inherits System.Windows.Forms.Form
Private Sub btnGen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGen.Click
Dim source As String
source = txtOne.Text & txtTwo.Text
Dim hash As String = getMd5Hash(source)
txtEnc.Text = hash
End Sub
End ClassThanks. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|