How to hash a string with SHA-256 in VB.NET

1 Answer

0 votes
Imports System
Imports System.Security.Cryptography
Imports System.Text

Module Module1

    Sub Main()
		Dim input As String = "VB.NET programming language"
        Dim hash As String = ComputeSha256(input)
        Console.WriteLine(hash)
    End Sub

    ' Reusable function that returns SHA‑256 hash as hex string
    Public Function ComputeSha256(input As String) As String
        Using sha As SHA256 = SHA256.Create()
            Dim bytes As Byte() = Encoding.UTF8.GetBytes(input)
            Dim hashBytes As Byte() = sha.ComputeHash(bytes)

            Dim sb As New StringBuilder()
            For Each b As Byte In hashBytes
                sb.Append(b.ToString("x2"))
            Next

            Return sb.ToString()
        End Using
    End Function

End Module



' run:
'
' beabcdbbaac14a1b40397eb6a962dd6abcccf1cdb7a0ecb6458963ec6acb2293 
'

 



answered 11 hours ago by avibootz
...