验证数据库中的salt

时间:2016-09-19 14:42:11

标签: mysql vb.net

我可以存储密码哈希和随机盐。我该如何验证密码?

    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

使用哈希和随机盐创建密码

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

GetSaltedHash(pw, dbSalt))

1 个答案:

答案 0 :(得分:0)

基本上,您使用哈希值保存salt,通常作为前缀 - 哈希不需要保密。

更好:使用bcrypt,它会为您完成所有操作,迭代并且安全 请参阅Plutonix的SO answer

简单地使用SHA512或任何哈希函数而不进行迭代是非常弱且易受攻击的。