0

I can store the password hashed and with random salt. How can i validate the password?

    Public Function GetSaltedHash(pw As String, salt As String) As String
    Dim tmp As String = pw & salt


    Using hash As HashAlgorithm = New SHA512Managed()

        Dim saltyPW = Encoding.UTF8.GetBytes(tmp)

        Dim hBytes = hash.ComputeHash(saltyPW)

        Return Convert.ToBase64String(hBytes)
    End Using
End Function

    Public Function CreateNewSalt(size As Integer) As String

    Using rng As New RNGCryptoServiceProvider

        Dim data(If(size < 7, 7, size)) As Byte

        rng.GetBytes(data)

        Return Convert.ToBase64String(data)
    End Using
End Function

Creating a password with hash and random salt

Const SaltSize As Integer = 31
Dim pw As String = txt_regpass.Text
Dim dbSalt = CreateNewSalt(SaltSize)

GetSaltedHash(pw, dbSalt))
Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178

1 Answers1

0

Essentially you save the salt with the hash value, generally as a prefix—the hash does not need to be secret.

Better: Use bcrypt, it will do it all for you, iterates and is secure.
See the SO answer by Plutonix.

Simply using SHA512 or any hash function without iteration is very weak and vulnerable.

Community
  • 1
  • 1
zaph
  • 111,848
  • 21
  • 189
  • 228
  • Where can i find the DLL files of bcrypt? How to import? – user6737469 Sep 19 '16 at 16:20
  • See SO question: [.net implementation of bcrypt](http://stackoverflow.com/q/873403/451475) and/or google. Other options in place of `bcrypt` include `scrypt` and `PBKDF2`. `PBKDF2` sometimes has a name similar to `RFC2898`. – zaph Sep 19 '16 at 16:53
  • 1
    @zaph PBKDF2 in the .NET framework is the [Rfc2898DeriveBytes Class](https://msdn.microsoft.com/en-us/library/system.security.cryptography.rfc2898derivebytes(v=vs.110).aspx). – Andrew Morton Sep 19 '16 at 17:36
  • Hi! I tried `bcrypt` but i'm having a hard time when verifying the input password and the hashed password from database. – user6737469 Sep 20 '16 at 16:48
  • See the SO answer by [Plutonix](http://stackoverflow.com/a/39602006/451475) – zaph Sep 20 '16 at 19:17