VB.NET Core 6,使用 SHA512 在(Win 窗体)中散列密码 - Visual Basic
VB.NET Core 6, Hashing password in (Win form) using SHA512 - Visual Basic
我正在尝试构建一个高度关注安全和加密的应用程序。
我正在使用 Visual Studio 2022 和 VB.NET 6.0(我搜索了3 天了,找不到合适的解决方案,我发现的所有内容都与不同版本的 .NET 有关,而不是 Visual Studio 2022)
更新:16/5/2022
我更新了我的问题,使其与我真正需要的更相关;这是哈希密码。
谢谢
这个解决方案对我很有用:
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
我正在尝试构建一个高度关注安全和加密的应用程序。
我正在使用 Visual Studio 2022 和 VB.NET 6.0(我搜索了3 天了,找不到合适的解决方案,我发现的所有内容都与不同版本的 .NET 有关,而不是 Visual Studio 2022)
更新:16/5/2022
我更新了我的问题,使其与我真正需要的更相关;这是哈希密码。
谢谢
这个解决方案对我很有用:
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