DEV Community

vblover programmer
vblover programmer

Posted on

VB .Net: Secure Password

SecurePassword Class:

Imports System.Text
Imports System.Security.Cryptography
Public Class SecurePassword
    Protected Friend Shared Function GetMd5Hash(ByVal input As String) As String
        ' Create a new instance of the MD5 object.
        Dim md5Hasher As MD5 = MD5.Create()
        ' Convert the input string to a byte array and compute the hash.
        Dim data As Byte() = md5Hasher.ComputeHash(Encoding.Default.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.
        Dim i As Integer
        For i = 0 To data.Length - 1
            sBuilder.Append(data(i).ToString("x2"))
        Next i
        ' Return the hexadecimal string.
        Return sBuilder.ToString()
    End Function
    ' Verify a hash against a string.
    Protected Friend Shared Function verifyMd5Hash(ByVal input As String, ByVal hash As String) As Boolean
        ' Hash the input.
        Dim hashOfInput As String = getMd5Hash(input)
        ' Create a StringComparer an compare the hashes.
        Dim comparer As StringComparer = StringComparer.OrdinalIgnoreCase
        If comparer.Compare(hashOfInput, hash) = 0 Then Return True
            Return False
     End Function
End Class

Enter fullscreen mode Exit fullscreen mode

Using for password property to save:

   Public ReadOnly Property Password() As String
        Get
            Return SecurePassword.GetMd5Hash(ConfirmPasswordBox.Text)
        End Get
    End Property

Enter fullscreen mode Exit fullscreen mode

Check for password correction:

If SecurePassword.verifyMd5Hash(Me.PasswordBox.Text,
UserAccount(Me.ID).Password) = False Then
                        Me.PasswordBox.Text = ""
                        .SetError(Me.PasswordBox, "Wrong Password!")
                        Me.PasswordBox.Focus()
                        Me.PasswordBox.Select()
                        Exit Sub
                    End If

Enter fullscreen mode Exit fullscreen mode

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay