我正在嘗試構建一個高度關注安全性和加密的應用程式。
我正在使用Visual Studio 2022和VB.NET 6.0(我現在搜索了 3 天,但找不到合適的解決方案,我發現的所有內容都與不同版本的 .NET 而不是 Visual Studio 2022 有關)
更新:2022 年 16 月 5 日
我更新了我的問題,使其與我真正需要的更相關;這是對密碼進行哈希處理。
謝謝
uj5u.com熱心網友回復:
這個解決方案像魅力一樣對我有用:
Imports System.Security.Cryptography
Imports System.Text
Public Module hashing
Public Function PWDhash(ByVal password As String)
Using sha512Hash As SHA512 = SHA512.Create()
Return GetHash(sha512Hash, password)
End Using
End Function
Private Function GetHash(ByVal hashAlgorithm As HashAlgorithm, ByVal input As String) As String
' Convert the input string to a byte array and compute the hash.
Dim data As Byte() = hashAlgorithm.ComputeHash(Encoding.UTF8.GetBytes(input))
' Create a new Stringbuilder to collect the bytes
' and create a string.
Dim sBuilder As New StringBuilder()
' Loop through each byte of the hashed data
' and format each one as a hexadecimal string.
For i As Integer = 0 To data.Length - 1
sBuilder.Append(data(i).ToString("x2"))
Next
' Return the hexadecimal string.
Return sBuilder.ToString()
End Function
' Verify a hash against a string.
Public Function VerifyHash(hashAlgorithm As HashAlgorithm, input As String, hash As String) As Boolean
' Hash the input.
Dim hashOfInput As String = GetHash(hashAlgorithm, input)
' Create a StringComparer an compare the hashes.
Dim comparer As StringComparer = StringComparer.OrdinalIgnoreCase
Return comparer.Compare(hashOfInput, hash) = 0
End Function
End Module
這是如何散列:
Dim HashedPWD As String = PWDhash("password here")
這是如何驗證:
Dim IsPWDCorrect As Boolean = VerifyHash(sha512Hash, "password here", "password hash from DB")
我還創建了一個強制用戶選擇復雜密碼的功能。
它適用于 VB.Net Core 6.0
哈希的長度為 128 位元組。
這是一個示例輸出:
708ed38ae70f96bc7dcb58515ab328614eaf3b41402de0c50e60ba0f56be5efc6f6daf0b226ec238c3dcaff182e466a1e12df1cadd4e62e6a8c197355b1edc4e
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/475934.html
