How to hash a password in VB.NET

1 Answer

0 votes
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)
    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

End Module



' run:
' 
' Hash: Ao0z5bhivJRPdJ1vL6YTHg==:K/S2LcEwXy92i27IzSE3IYYij8IxbIbw8I4y/skeub4=
' 

 



answered 15 hours ago by avibootz
...