Imports System
Imports System.Security.Cryptography
Module Program
Sub Main()
Dim password As String = "SecurePassword123!@"
Dim storedHash As String = HashPassword(password)
Console.Write("Hash: ")
Console.WriteLine(storedHash)
Dim passwordToVerify As String = "SecurePassword123!@"
If VerifyPassword(passwordToVerify, storedHash) Then
Console.WriteLine("Password is correct")
Else
Console.WriteLine("Invalid password")
End If
End Sub
' Hash a password using PBKDF2-HMAC-SHA256
Public Function HashPassword(password As String) As String
Dim salt(15) As Byte
Using rng As RandomNumberGenerator = RandomNumberGenerator.Create()
rng.GetBytes(salt)
End Using
Dim pbkdf2 As New Rfc2898DeriveBytes(
password,
salt,
100000,
HashAlgorithmName.SHA256
)
Dim hash As Byte() = pbkdf2.GetBytes(32)
Return Convert.ToBase64String(salt) & ":" & Convert.ToBase64String(hash)
End Function
' Verify a password
Public Function VerifyPassword(password As String, stored As String) As Boolean
Dim parts = stored.Split(":"c)
Dim salt As Byte() = Convert.FromBase64String(parts(0))
Dim storedHash As Byte() = Convert.FromBase64String(parts(1))
Dim pbkdf2 As New Rfc2898DeriveBytes(
password,
salt,
100000,
HashAlgorithmName.SHA256
)
Dim newHash As Byte() = pbkdf2.GetBytes(32)
Return CryptographicOperations.FixedTimeEquals(storedHash, newHash)
End Function
End Module
' run:
'
' Hash: 9+chUmhYs7o9qEn8Km8ctw==:fzindcOLNkB1DtGoBnvpKqx6dRAW+Fuhapu6JpSHuh8=
' Password is correct
'