Generate a hash from a string

Posted on 2009-04-21
Last Modified: 2012-05-06

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.

Question by:uxphreak
    LVL 39

    Accepted Solution

    Actually, it is quite straightforward. Check this link: (C#)

    In VB that would be:

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

    Imports System.Security.Cryptography
    on top of your class.

    Author Comment

    Thanks abel, but isn't GetSHA256 just for C#?  I'm using VB and receive an error when using GetSHA256.
    LVL 15

    Assisted Solution

    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")

    LVL 15

    Expert Comment

    Developer Fusion has a great tool that translates C# <--> VB.Net
    LVL 85

    Assisted Solution

    by:Mike Tomlinson
    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
    LVL 39

    Expert Comment

    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 :)

    Author Comment

    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
            Next outputByte
            Return sb.ToString.ToLower
    End Function
    End Class

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    When the confidentiality and security of your data is a must, trust the highly encrypted cloud fax portfolio used by 12 million businesses worldwide, including nearly half of the Fortune 500.
    Even if you have implemented a Mobile Device Management solution company wide, it is a good idea to make sure you are taking into account all of the major risks to your electronic protected health information (ePHI).
    As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
    Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now