passwordStrength method

String passwordStrength()

Determines the strength of a password.

Strength Levels:

  • Weak → Password is shorter than 6 characters.
  • Medium → At least 6 characters and contains:
    • One uppercase letter
    • One lowercase letter
    • One number
  • Strong → At least 8 characters and contains:
    • One uppercase letter
    • One lowercase letter
    • One number
    • One special character (@$!%*?&)

Example Usage:

print("abc".passwordStrength());          // Weak (too short)
print("Abc123".passwordStrength());       // Medium (meets basic rules)
print("StrongP@ssword1".passwordStrength()); // Strong (meets all conditions)
print("password".passwordStrength());     // Weak (no numbers or uppercase)

Edge Cases:

  • Passwords without uppercase, lowercase, or numbers remain weak.
  • Passwords with special characters but fewer than 8 characters are still medium.
  • The method does not enforce extra security policies like no repeated characters.

Implementation

String passwordStrength() {
  if (length < 6) return 'Weak';
  if (RegExp(r'^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{6,}$').hasMatch(this)) {
    return 'Medium';
  }
  if (RegExp(r'^(?=.*[A-Z])(?=.*[a-z])(?=.*\d)(?=.*[@$!%*?&]).{8,}$')
      .hasMatch(this)) {
    return 'Strong';
  }
  return 'Weak';
}