String --> SHA1 hash (in hexadecimal)

  • Thread starter Thread starter Al Murphy
  • Start date Start date
A

Al Murphy

Folks,

I've written what I believe to be a conversion utility. It takes
the string from an input field and converts it into a SHA1 hash and
then outputs said hash (in hex) to the next tetxfield. The step taken
are as follows:

1. Capture string from input textfield
2. Convert string to byte array
3. Convert byte array to SHA1 hash
4. Convert SHA1 hash to hex
5. Output result

I've included the code below and would be interested in your
comments/suggestions/ideas/anything else that you would care to share.

Al.

CODE AS FOLLOWS:

Dim strPlaintext As String

Dim byteArray As Byte()

Dim textEncoder As New System.Text.ASCIIEncoding()

' Grab the plaintegxt from the input field
strPlaintext = TextBox1.Text()
' Convert string -> byte array
byteArray = textEncoder.GetBytes(strPlaintext)

'Convert byte array to SHA1 hash
Dim result() As Byte
Dim shaM As New SHA512Managed()
result = shaM.ComputeHash(byteArray)

'Convert byte array to hexadecimal string
Dim hexString As String = ""
Dim i As Integer
For i = 0 To (result.Length - 1)
hexString = hexString + Hex(result(i))
Next i

' Output result to second testfield
TextBox2.Text = hexString
 
Thanks, useful. One comment tho (for newbies?):
use either
Import System.Security.Cryptography
or
Dim shaM As New System.Security.Cryptography.SHA512Managed()
 
Back
Top