Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 937
  • Last Modified:

Generate a hash from a string

Hello,

I have an old VB 6 application that uses a class to create a hash of a users password which is output and stored in a database.  I need to create a VB.NET 2008 application that needs to compare the hash value in the database but I don't know how to create the function to generate the hash.

The hash is generated from a string consisting of input from the user along with a value appended to the input.  I was told by the vendor who created the class in the VB 6 application that I should use SHA256MANAGED in VB.NET 2008 to generate the hash but they have refused to help me any further than that.  They have provided the VB 6 Class code to me, but with my inexperience I can't make heads or tales of it.  I can attach the code if this helps to determine the solution.

I realize this might be a futile effort but I'm hoping this is less complicated than I fear it is.

Thanks.
0
uxphreak
Asked:
uxphreak
  • 2
  • 2
  • 2
  • +1
3 Solutions
 
abelCommented:
Actually, it is quite straightforward. Check this link: http://www.developerfusion.com/code/4601/create-hashes-md5-sha1-sha256-sha384-sha512/ (C#)

In VB that would be:

Dim hashedString As String = GetSHA256(yourString)
and place an

Imports System.Security.Cryptography
on top of your class.
0
 
uxphreakAuthor Commented:
Thanks abel, but isn't GetSHA256 just for C#?  I'm using VB and receive an error when using GetSHA256.
0
 
oobaylyCommented:
The to add a point, make sure that you use the same encoding that was used in the vb6 app:
System.Text.Encoding.Default.GetBytes("foo") <> System.Text.Encoding.Unicode.GetBytes("foo")

0
Cyber Threats to Small Businesses (Part 1)

This past May, Webroot surveyed more than 600 IT decision-makers at medium-sized companies to see how these small businesses perceived new threats facing their organizations.  Read what Webroot CISO, Gary Hayslip, has to say about the survey in part 1 of this 2-part blog series.

 
oobaylyCommented:
Developer Fusion has a great tool that translates C# <--> VB.Net
http://www.developerfusion.com/tools/convert/csharp-to-vb/
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Putting it together...

        Dim strToHash As String = TextBox1.Text

        Dim SHA256 As New System.Security.Cryptography.SHA256Managed
        Dim hash() As Byte = SHA256.ComputeHash(System.Text.Encoding.Default.GetBytes(strToHash))
        Dim strHash As String = Convert.ToBase64String(hash)

        Label1.Text = strHash
0
 
abelCommented:
thanks for the follow-up on this and filling in the blanks, guys. I was a bit brief and not as complete as I could be. Hope the combination of comments helps the OP :)
0
 
uxphreakAuthor Commented:
Thanks everyone.  Using the comments and suggestions, I tailored my code to suite my needs...
Imports System
Imports System.Text
Imports System.Security.Cryptography
 
Public Class clsEncrypt
 
Public Function SHA256MHash(ByVal strPWD as String) as String
 
        Dim strToHash As String = strPWD & "EIOOWC32KGF02LG"
        Dim SHA256 As New SHA256Managed
        Dim hash() As Byte = sHA256.ComputeHash(Encoding.ASCII.GetBytes(strToHash))
        Dim sb as New Stringbuilder
        Dim outputByte as Byte
        For Each outputByte in hash
            sb.Append(outputByte.ToString("x2").ToUpper)
        Next outputByte
 
        Return sb.ToString.ToLower
 
End Function
 
End Class

Open in new window

0

Featured Post

Threat Trends for MSPs to Watch

See the findings.
Despite its humble beginnings, phishing has come a long way since those first crudely constructed emails. Today, phishing sites can appear and disappear in the length of a coffee break, and it takes more than a little know-how to keep your clients secure.

  • 2
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now