+ Reply to Thread
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 15

Thread: Salted Password Hash

  1. #1
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Washington, D.C.
    Posts
    2,347
    Blog Entries
    9
    Rep Power
    19

    Salted Password Hash

    For added security, it is a good idea to use a salted password hash. Below are a few methods that I use in my applications to create the salt and password hash.

    Create Salt:

    Code:
            Public Shared Function CreateSalt(ByVal size As Integer) As String
                ' Generate a cryptographic random number using the cryptographic
                ' service provider
                Dim rng As RNGCryptoServiceProvider = New RNGCryptoServiceProvider
                Dim buff As Byte() = New Byte(size) {}
                rng.GetBytes(buff)
     
                ' Return a Base64 string representation of the random number
                Return Convert.ToBase64String(buff)
            End Function
    
    Create the salted password hash:
    Code:
            Public Shared Function CreatePasswordHash(ByVal pwd As String, ByVal salt As String) As String
                Dim saltandPwd As String = String.Concat(pwd, salt)
                Dim hashedPwd As String = FormsAuthentication.HashPasswordForStoringInConfigFile(saltandPwd, "SHA1")
                hashedPwd = String.Concat(hashedPwd, salt)
     
                Return hashedPwd
            End Function
    
    Required Database fields (2):
    Salt varcharchar (200).
    PasswordHash varchar (200).

    To Use:
    When creating a new user, call CreateSalt() to generate a new salt for the user. Then call CreatePasswordHash to hash the password and salt together.

    When authenticating a user, retrieve the salt and hashed password from the database. Pass the password supplied from the user and the salt retrieved from the database through CreatePasswordHash(). Compare that value to the hashed password retrieved from the database.

    That's it! Keep in mind, this all happens on the server side. Anything can be intercepted when sent from the client unless sent over a secure connection.
    jmurrayhead
    If you agree, give me rep. If my post helped you, click "Thanks".
    If you like it here...throw us a few bones to help support us.


  2. #2
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    1,724
    Blog Entries
    10
    Rep Power
    11

    I tried using this code but when I attempt to log back in, it won't match the passwords. When I rehash the entered password using the Salt from the db it doesn't match the hashed password stored in the database.

    I'm using MySQL with a VARCHAR (8) field for Salt and VARCHAR (48) field for password hash and have a stored procedure that adds the user to the db.

    Am I missing something?

  3. #3
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Washington, D.C.
    Posts
    2,347
    Blog Entries
    9
    Rep Power
    19

    Hey rr,

    Please post how you're using the above methods.
    jmurrayhead
    If you agree, give me rep. If my post helped you, click "Thanks".
    If you like it here...throw us a few bones to help support us.


  4. #4
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    1,724
    Blog Entries
    10
    Rep Power
    11

    OK. When I add a user I have this:-
    Code:
                Dim salt As String = CreateSalt(8)
                            Dim passwordHash As MySqlParameter = mycomm.Parameters.Add("passwordHash", MySqlDbType.VarChar)
                            passwordHash.Value = CreatePasswordHash(user.password, salt)
                            Dim psalt As MySqlParameter = mycomm.Parameters.Add("salt", MySqlDbType.VarChar)
                            psalt.Value = salt
    
    CreateSalt
    Code:
            Public Shared Function CreateSalt(ByVal size As Integer) As String
                ' Generate a cryptographic random number using the cryptographic
                ' service provider
                Dim rng As RNGCryptoServiceProvider = New RNGCryptoServiceProvider
                Dim buff As Byte() = New Byte(size) {}
                rng.GetBytes(buff)
                ' Return a Base64 string representation of the random number
                Return Convert.ToBase64String(buff)
            End Function
    
    CreatePasswordHash
    Code:
            Public Shared Function CreatePasswordHash(ByVal pwd As String, ByVal salt As String) As String
                Dim saltandPwd As String = String.Concat(pwd, salt)
                Dim hashedPwd As String = FormsAuthentication.HashPasswordForStoringInConfigFile(saltandPwd, "SHA1")
                hashedPwd = String.Concat(hashedPwd, salt)
                Return hashedPwd
            End Function
    
    Then to check the password I have
    Code:
          Public Shared Function CheckLogin(ByVal email As String, ByVal password As String) As UserBOL
              Dim result As New UserBOL
              Dim conn As New MySqlConnection(ConnDAL.connString)
              Dim mycomm As New MySqlCommand("SELECT userref,salt,passwordHash FROM users WHERE email=?email", conn)
              Dim rs As MySqlDataReader
              Using conn
                  Using mycomm
                      Try
                          mycomm.Parameters.AddWithValue("email", email)
                          conn.Open()
                          rs = mycomm.ExecuteReader
                          If rs.HasRows Then
                              rs.Read()
                              If CreatePasswordHash(password, rs("salt")) = rs("passwordHash") Then
                                  'authenticated
                                  result.userref = rs("userref")
                              Else
     'It always ends up here
                                  result.userref = 10
                                  result.email = CreatePasswordHash(password, rs("salt"))
                              End If
                          Else
                              result.userref = 9
                          End If
                          rs.Close()
                      Catch ex As Exception
                          result.err = ex.ToString
                          result.userref = 11
                      Finally
                          mycomm.Dispose()
                          conn.Close()
                          conn.Dispose()
                      End Try
                   End Using
               End Using
     
    Return result
     
    End Function
     

    Last edited by richyrich; December 22nd, 2008 at 11:33 AM.

  5. #5
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Washington, D.C.
    Posts
    2,347
    Blog Entries
    9
    Rep Power
    19

    Okay RR...the problem is that I provided the field length for the salt based on the value (size) I was using. For size of 8 bytes, you need a salt field of 12 characters.

    You will have to resize this field, create a new hash and then run this again. You will also have to resize the hashed password field. I will fix my first post.
    jmurrayhead
    If you agree, give me rep. If my post helped you, click "Thanks".
    If you like it here...throw us a few bones to help support us.


  6. #6
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    1,724
    Blog Entries
    10
    Rep Power
    11

    Hey J

    Not sure I quite get it.....I've changed the Salt field to 12 characters and the password hash to 52, but it still doesn't match....And only 8 characters are saved in the db for the Salt.

    Have I misunderstood?

  7. #7
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Washington, D.C.
    Posts
    2,347
    Blog Entries
    9
    Rep Power
    19

    Quote Originally Posted by richyrich View Post
    Hey J

    Not sure I quite get it.....I've changed the Salt field to 12 characters and the password hash to 52, but it still doesn't match....And only 8 characters are saved in the db for the Salt.

    Have I misunderstood?
    Did you delete the record and recreate the user? Have you tried debugging using Visual Studio or Visual Web Developer to see what values you are comparing?
    jmurrayhead
    If you agree, give me rep. If my post helped you, click "Thanks".
    If you like it here...throw us a few bones to help support us.


  8. #8
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    1,724
    Blog Entries
    10
    Rep Power
    11

    Yep. I removed the user and added them again.

    I presume this doesn't change?
    Code:
    Dim salt as string = CreateSalt(8)
    
    I still only get 8 characters saved in the Salt field.

    What should I debug?...I have passed the hashed password back to the screen and I can see it doesn't match the password stored in the db.

  9. #9
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Washington, D.C.
    Posts
    2,347
    Blog Entries
    9
    Rep Power
    19

    Quote Originally Posted by richyrich View Post
    Yep. I removed the user and added them again.

    I presume this doesn't change?
    Code:
    Dim salt as string = CreateSalt(8)
    
    I still only get 8 characters saved in the Salt field.

    What should I debug?...I have passed the hashed password back to the screen and I can see it doesn't match the password stored in the db.
    Have you changed the parameter size in the stored procedure and in VB.Net to the correct size?
    jmurrayhead
    If you agree, give me rep. If my post helped you, click "Thanks".
    If you like it here...throw us a few bones to help support us.


  10. #10
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    1,724
    Blog Entries
    10
    Rep Power
    11

    OK. That seemed to sort it...

    Thanks J...

+ Reply to Thread
Page 1 of 2 1 2 LastLast

Similar Threads

  1. Hashing (Encryption) Password & Other Sensitive Information
    By BLaaaaaaaaaarche in forum ASP Code Samples
    Replies: 5
    Last Post: February 23rd, 2009, 10:59 PM
  2. Replies: 20
    Last Post: November 4th, 2008, 08:56 AM
  3. Password Protected Tabs
    By AOG123 in forum Access Database Samples
    Replies: 3
    Last Post: June 13th, 2008, 03:20 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

SEO by vBSEO