checkPasswordStrength static method

PasswordStrength checkPasswordStrength(
  1. String password
)

Checks the strength of a password and returns a PasswordStrength value.

Implementation

static PasswordStrength checkPasswordStrength(String password) {
  int score = 0;

  // Length checks
  if (password.length >= 8) score++;
  if (password.length >= 12) score++;

  // Character variety checks
  if (RegExp(r'[a-z]').hasMatch(password)) score++;
  if (RegExp(r'[A-Z]').hasMatch(password)) score++;
  if (RegExp(r'[0-9]').hasMatch(password)) score++;
  if (RegExp(r'[!@#$%^&*(),.?":{}|<>]').hasMatch(password)) score++;

  switch (score) {
    case 0:
    case 1:
    case 2:
      return PasswordStrength.weak;
    case 3:
    case 4:
      return PasswordStrength.medium;
    case 5:
      return PasswordStrength.strong;
    default:
      return PasswordStrength.veryStrong;
  }
}