Skip to content

Instantly share code, notes, and snippets.

@frankradocaj
Last active February 29, 2016 01:23
Show Gist options
  • Save frankradocaj/12be39c000c467e4a373 to your computer and use it in GitHub Desktop.
Save frankradocaj/12be39c000c467e4a373 to your computer and use it in GitHub Desktop.
Password Evaluator
namespace TehSecureSystem
{
public class PasswordEvaluator
{
public int MinLength { get; }
public int MinLowercaseChars { get; }
public int MinUppercaseChars { get; }
public int MinSpecialChars { get; }
public int MinNumberChars { get; }
public PasswordEvaluator(
int minLength,
int minLowercaseChars,
int minUppercaseChars,
int minSpecialChars,
int minNumberChars)
{
MinLength = minLength;
MinLowercaseChars = minLowercaseChars;
MinUppercaseChars = minUppercaseChars;
MinSpecialChars = minSpecialChars;
MinNumberChars = minNumberChars;
}
public bool IsStrongEnough(Password password)
{
return CalculateScore(password) == 5;
}
public bool IsLongEnough(Password password)
{
return password.Length >= MinLength;
}
public int CalculateScore(Password password)
{
var score = 0;
// Minimum length requirement
if (password.Length >= MinLength)
{
score++;
}
// Mixed case requirement
if (password.TotalLowercaseChars >= MinLowercaseChars)
{
score++;
}
// Mixed case requirement
if (password.TotalUppercaseChars >= MinUppercaseChars)
{
score++;
}
// Special chars requirement
if (password.TotalSpecialChars > MinSpecialChars)
{
score++;
}
// Number chars requirement
if (password.TotalNumberChars >= MinNumberChars)
{
score++;
}
return score;
}
}
public class Password
{
// Default settings
public static PasswordEvaluator Evaluator = new PasswordEvaluator(6, 1, 1, 1, 1);
public Password(string value)
{
Value = value == null ? string.Empty : value.Trim();
Length = Value.Length;
foreach (var c in Value)
{
var charCode = (int)c;
if (charCode >= 48 && charCode <= 57) TotalNumberChars++;
else if (charCode >= 65 && charCode <= 90) TotalUppercaseChars++;
else if (charCode >= 97 && charCode <= 122) TotalLowercaseChars++;
else TotalSpecialChars++;
}
}
public string Value { get; }
public int Length { get; }
public int TotalNumberChars { get; }
public int TotalUppercaseChars { get; }
public int TotalLowercaseChars { get; }
public int TotalSpecialChars { get; }
public static bool IsStrongEnough(string password)
{
var passwordObj = new Password(password);
return Evaluator.IsStrongEnough(passwordObj);
}
public static int Strength(string password)
{
var passwordObj = new Password(password);
return Evaluator.CalculateScore(passwordObj);
}
public static bool IsLongEnough(string password)
{
var passwordObj = new Password(password);
return Evaluator.IsLongEnough(passwordObj);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment