Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6650257
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:52:17+00:00 2026-05-26T00:52:17+00:00

I’m using the same function to hash values for comparison during login as I

  • 0

I’m using the same function to hash values for comparison during login as I am to hash the passwords when users register:

Public Shared Function Compute(ByVal text As String, ByVal algorithm As String, Optional ByVal salt() As Byte = Nothing) As String
    If salt Is Nothing Then
        Dim saltSize As Integer = 8
        salt = New Byte(saltSize - 1) {}

        Dim rng As New RNGCryptoServiceProvider
        rng.GetNonZeroBytes(salt)
    End If

    Dim textBytes As Byte() = Encoding.UTF8.GetBytes(text)
    Dim saltedTextBytes() As Byte = New Byte(textBytes.Length + salt.Length - 1) {}
    For i As Integer = 0 To textBytes.Length - 1
        saltedTextBytes(i) = textBytes(i)
    Next i

    For i As Integer = 0 To salt.Length - 1
        saltedTextBytes(textBytes.Length + i) = salt(i)
    Next i

    Dim hash As HashAlgorithm
    If algorithm Is Nothing Then
        algorithm = ""
    End If

    Select Case algorithm.ToUpper
        Case "SHA1" : hash = New SHA1Managed
        Case "SHA256" : hash = New SHA256Managed
        Case "SHA384" : hash = New SHA384Managed
        Case "SHA512" : hash = New SHA512Managed
        Case Else : hash = New MD5CryptoServiceProvider
    End Select

    Dim hashBytes As Byte() = hash.ComputeHash(saltedTextBytes)
    Dim saltedHash() As Byte = New Byte(hashBytes.Length + salt.Length - 1) {}
    For i As Integer = 0 To hashBytes.Length - 1
        saltedHash(i) = hashBytes(i)
    Next i

    For i As Integer = 0 To salt.Length - 1
        saltedHash(hashBytes.Length + i) = salt(i)
    Next i

    Dim hashValue As String = Convert.ToBase64String(saltedHash)

    Return Left(hashValue, 36)
End Function

My problem is that when I try to log in on an account whose password was hashed by this function, the hashed values don’t match up. I think I’m skipping a step or something.

Here’s the code for user account creation:

        ' The email address needs to be valid
        Dim pattern As String = "^(?("")("".+?""@)|(([0-9a-zA-Z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-zA-Z])@))(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,6}))$"
        Dim match As Match = Regex.Match(txtEmail.Text, pattern)
        If match.Success Then
            'Hash the user's password before entering it into the database.
            Dim pass As String = Crypt.Compute(txtPass.Text, "SHA512", Nothing)

            ' Enter the information from the form into the database.
            Dim sql As String = "INSERT INTO Users(Username, Password, EmailAddress) " & _
                "VALUES(@User, @Pass, @Email)"
            Dim cmd As New SqlCommand(sql, conn)
            cmd.Parameters.AddWithValue("@User", txtName.Text)
            cmd.Parameters.AddWithValue("@Pass", pass)
            cmd.Parameters.AddWithValue("@Email", txtEmail.Text)

            conn.Open()
            cmd.ExecuteNonQuery()
            conn.Close()
        Else
            lblError.Text = "Invalid email address. Please correct."
            lblError.ForeColor = Drawing.Color.Red
        End If

There are more checks that aren’t included here that aren’t relevant to my problem.

Here’s my user login:

            Dim pass As String = Crypt.Compute(txtPass.Text, "SHA512", Nothing)

            Dim UserData As New DataSet
            Dim UserAdapter As New SqlDataAdapter
            UserAdapter.SelectCommand = New SqlCommand("SELECT * FROM Users " & _
                                                       "WHERE Username = @User AND Password = @Pass", conn)
            UserAdapter.SelectCommand.Parameters.AddWithValue("@User", txtUser.Text)
            UserAdapter.SelectCommand.Parameters.AddWithValue("@Pass", pass)
            UserAdapter.Fill(UserData)

            If UserData.Tables(0).Rows.Count <> 1 Then
                lblError.Text = "Invalid username or password."
                lblError.ForeColor = Drawing.Color.Red
                Session("LoginAttempt") = CInt(Session("LoginAttempt")) + 1
            Else
                Session("LoggedIn") = True
                Response.Redirect("Home.aspx")
            End If

As far as I can see, there is no difference in the hashing I’ve done here.

Does anyone have any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T00:52:17+00:00Added an answer on May 26, 2026 at 12:52 am
    1. When you creating an account by inserting into the table, you are using txtName.Text for the username, but when checking the credentials you are using txtUser.Text.
    2. Why are you using a random salt? Doesn’t the salt have to be the same for every encryption? I’ve pasted your code into a new project, and when I run the Compute method twice in a row for the same password, I get two different results… obviously that won’t work. Try passing in a salt value instead of Nothing, and use the same salt for creating accounts and comparing login. Here’s some sample code that works:

      Dim thePass As String = "MyPassword"
      Dim theSalt As String = "salt"
      
      Dim pass As String = Compute(thePass, "SHA512", Encoding.UTF8.GetBytes(theSalt))
      Console.WriteLine(pass)
      Dim pass2 As String = Compute(thePass, "SHA512", Encoding.UTF8.GetBytes(theSalt))
      Console.WriteLine(pass2) 'pass and pass2 are identical
      

    Hope this helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
In order to apply a triggered animation to all ToolTip s in my app,
I want use html5's new tag to play a wav file (currently only supported
I want to count how many characters a certain string has in PHP, but
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.