2016-09-19 22 views
0

我可以存儲密碼哈希和隨機鹽。我如何驗證密碼?驗證數據庫中的鹽

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

代碼來自的答案解釋瞭如何:通過相同的散列機制運行密碼嘗試,並將結果與​​存儲的結果進行比較。你不驗證鹽,但PW哈希 - 你將需要使用最初用來散列已保存的PW的鹽。 – Plutonix

+0

你讀過這個了嗎? http://stackoverflow.com/questions/1219899/where-do-you-store-your-salt-strings –

+1

這個密碼哈希代碼非常弱。至少你應該使用[Bcrypt](http://bcrypt.codeplex.com)。 – tadman

回答

0

基本上你,保存鹽與哈希值一般爲前綴的散列並不需要是祕密。

更好:使用bcrypt,它會爲您完成所有工作,迭代並且安全。
請參閱Plutonix的SO answer

僅僅使用SHA512或任何散列函數沒有迭代是非常脆弱和脆弱的。

+0

我在哪裏可以找到bcrypt的DLL文件? 如何導入? – user6737469

+0

看到SO問題:[.net的bcrypt實現](http://stackoverflow.com/q/873403/451475)和/或谷歌。其他替代'bcrypt'的選項包括'scrypt'和'PBKDF2'。 'PBKDF2'有時候有一個類似於'RFC2898'的名字。 – zaph

+1

@zaph在.NET框架中的PBKDF2是[Rfc2898DeriveBytes Class](https://msdn.microsoft.com/en-us/library/system.security.cryptography.rfc2898derivebytes(v = vs.110).aspx)。 –