我可以存储密码哈希和随机盐。我该如何验证密码?
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))