다음을 통해 공유


VB.Net - Password Strength Meter


Overview

This is a simple Password Strength Meter, consisting of a TextBox to enter a password, with a maxlength of 20, which uses * as the Password Character.

There's a second TextBox with the same attributes to confirm the password you've entered.

There's a label with a tick character that indicates whether your passwords match, and a graphical strength display.

Scoring is simple. Each character entered is worth 2.5 points. Additionally there are bonus points for different types of character used.

  • lowercase
  • uppercase
  • numbers
  • symbols (or punctuation)

Using 2 of these types scores an extra 20 points

Using 3 of these types scores an extra 30 points

Using 4 of these types scores an extra 50 points

Therefore there's a minimum of 2.5 points if you just enter 1 character, and a maximum of 100 points if you enter 20 characters consisting of all 4 types.

Scores range from 'Poor' to 'Sufficient' to 'Excellent'

This is a very simple but fairly effective algorithm. As an example, an input of 'abc' would score 7.5 points, whereas 'abc123' would score 15 points for the character count plus a bonus of 20 points for using 2 types of character, therefore an overall score of 35.


Code

This is a simple method, called from the TextChanged events of both TextBoxes, that toggles visibility of the check mark label and sets Enabled for the Clear Button

Private Sub  formfeatures(ByVal  pwf1 As  TextBox, ByVal  pwf2 As  TextBox)
    Label4.Visible = pwf1.Text.Length > 0 AndAlso  pwf2.Text.Equals(pwf1.Text)
    Button2.Enabled = pwf1.Text.Length > 0 OrElse  pwf2.Text.Length > 0
End Sub

There are two buttons. One is the Close Button, and the other is the Clear Button that removes all of the text and the image.

Private Sub  Button1_Click(sender As Object, e As  EventArgs) Handles  Button1.Click
    Application.Exit()
End Sub
 
Private Sub  Button2_Click(sender As Object, e As  EventArgs) Handles  Button2.Click
    TextBox1.Clear()
    TextBox2.Clear()
    Label4.Visible = False
    Button2.Enabled = False
End Sub

There are two TextChanged handlers. The first is for the first password TextBox. The code in this handler calculates the password strength, then creates a strength display image, which is then loaded into the PictureBox.
The second (and also the first) TextChanged handler calls the formfeatures method.

Private Sub  TextBox1_TextChanged(sender As Object, e As  EventArgs) Handles  TextBox1.TextChanged
    formfeatures(TextBox1, TextBox2)
    'Stop
    Dim passwordStrength As Decimal  = TextBox1.Text.Length * 2.5
 
    Dim count As New  Dictionary(Of String, Integer)
    count.Add("lowercase", 0)
    count.Add("uppercase", 0)
    count.Add("numbers", 0)
    count.Add("symbols", 0)
 
    For x As Integer  = 0 To  TextBox1.Text.Length - 1
        If Char.IsLetter(TextBox1.Text(x)) And  Char.IsLower(TextBox1.Text(x)) Then
            count("lowercase") += 1
        End If
        If Char.IsLetter(TextBox1.Text(x)) And  Char.IsUpper(TextBox1.Text(x)) Then
            count("uppercase") += 1
        End If
        If Char.IsDigit(TextBox1.Text(x)) Then
            count("numbers") += 1
        End If
        If Char.IsSymbol(TextBox1.Text(x)) Or  Char.IsPunctuation(TextBox1.Text(x)) Then
            count("symbols") += 1
        End If
    Next
 
    Dim c As Integer  = 0
    For Each  kvp As  KeyValuePair(Of String, Integer) In  count
        c += If(kvp.Value > 0, 1, 0)
    Next
 
    passwordStrength += (If(c = 2, 20, (If(c = 3, 30, (If(c = 4, 50, 0))))))
 
    If passwordStrength > 0 Then
 
        Dim img As New  Bitmap(300, 20)
        Dim gr As Graphics = Graphics.FromImage(img)
        gr.Clear(SystemColors.Control)
 
        Dim color As Color = (If(passwordStrength < 50, Color.IndianRed, (If(passwordStrength < 85, Color.GreenYellow, Color.Gold))))
        Dim brush As New  LinearGradientBrush(New Rectangle(Point.Empty, New Size(passwordStrength * 3 + 50, 20)), color, SystemColors.Control, LinearGradientMode.Horizontal)
        gr.FillRectangle(brush, New  Rectangle(Point.Empty, New Size(passwordStrength * 3 + 50, 20)))
        gr.DrawString((If(passwordStrength < 50, "Poor", (If(passwordStrength < 85, "Sufficient",  "Excellent")))),  Me.Font, Brushes.Black, 6, 3)
 
        PictureBox1.Image = img
    Else
        PictureBox1.Image = Nothing
    End If
End Sub
 
Private Sub  TextBox2_TextChanged(sender As Object, e As  EventArgs) Handles  TextBox2.TextChanged
    formfeatures(TextBox1, TextBox2)
End Sub

Conclusion

Writing a small example program such as this, would take an experienced developer anything between 30 minutes and 2 hours, the most difficult part being the graphical score display. A beginner programmer would probably take significantly longer without knowledge of the Properties and methods used.


Download

Download here...


See Also

C#2017 version