C#: 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 void formfeatures(TextBox pwf1, TextBox pwf2)
{
Label4.Visible = pwf1.Text.Length > 0 && pwf2.Text.Equals(pwf1.Text);
Button2.Enabled = pwf1.Text.Length > 0 || pwf2.Text.Length > 0;
}
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 void Button1_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void Button2_Click(object sender, EventArgs e)
{
TextBox1.Clear();
TextBox2.Clear();
Label4.Visible = false;
Button2.Enabled = false;
}
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 void TextBox1_TextChanged(object sender, EventArgs e)
{
formfeatures(TextBox1, TextBox2);
//Stop
decimal passwordStrength = Convert.ToDecimal(TextBox1.Text.Length * 2.5);
Dictionary<string, int> count = new Dictionary<string, int>();
count.Add("lowercase", 0);
count.Add("uppercase", 0);
count.Add("numbers", 0);
count.Add("symbols", 0);
for (int x = 0; x < TextBox1.Text.Length; x++)
{
if (char.IsLetter(TextBox1.Text[x]) & char.IsLower(TextBox1.Text[x]))
{
count["lowercase"] += 1;
}
if (char.IsLetter(TextBox1.Text[x]) & char.IsUpper(TextBox1.Text[x]))
{
count["uppercase"] += 1;
}
if (char.IsDigit(TextBox1.Text[x]))
{
count["numbers"] += 1;
}
if (char.IsSymbol(TextBox1.Text[x]) | char.IsPunctuation(TextBox1.Text[x]))
{
count["symbols"] += 1;
}
}
int c = 0;
foreach (KeyValuePair<string, int> kvp in count)
{
c += (kvp.Value > 0) ? 1 : 0;
}
passwordStrength += ((c == 2) ? 20 : ((c == 3) ? 30 : ((c == 4) ? 50 : 0)));
if (passwordStrength > 0M)
{
Bitmap img = new Bitmap(300, 20);
Graphics gr = Graphics.FromImage(img);
gr.Clear(SystemColors.Control);
Color color = ((passwordStrength < 50M) ? Color.IndianRed : ((passwordStrength < 85M) ? Color.GreenYellow : Color.Gold));
LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(Point.Empty, new Size(Convert.ToInt32(passwordStrength * 3 + 50), 20)), color, SystemColors.Control, LinearGradientMode.Horizontal);
gr.FillRectangle(brush, new Rectangle(Point.Empty, new Size(Convert.ToInt32(passwordStrength * 3 + 50), 20)));
gr.DrawString(((passwordStrength < 50M) ? "Poor" : ((passwordStrength < 85M) ? "Sufficient" : "Excellent")), this.Font, Brushes.Black, 6, 3);
PictureBox1.Image = img;
}
else
{
PictureBox1.Image = null;
}
}
private void TextBox2_TextChanged(object sender, EventArgs e)
{
formfeatures(TextBox1, TextBox2);
}
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.