clsPassword.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Text.RegularExpressions;
6 using System.Drawing;
7 using sage.ew.global;
8 
9 namespace sage.ew.usuario
10 {
14  public static class Password
15  {
19  public enum FortalezaPassword
20  {
24  Blanco = 0,
28  MuyDebil = 1,
32  Debil = 2,
36  Media = 3,
40  Fuerte = 4,
44  MuyFuerte = 5
45  }
46 
50  public static int _Longitud_Minima
51  {
52  get { return _nLongitudMinima; }
53  //set { _nLongitudMinima = value; }
54  }
55  private static int _nLongitudMinima = 8;
56 
60  public static int _Longitud_Maxima
61  {
62  get { return _nLongitudMaxima; }
63  //set { _nLongitudMaxima = value; }
64  }
65  private static int _nLongitudMaxima = 20;
66 
70  public static int _Validez_Minima
71  {
72  get { return _nValidezMinima; }
73  }
74  private static int _nValidezMinima = 30;
75 
79  public static int _Validez_Maxima
80  {
81  get { return _nValidezMaxima; }
82  }
83  private static int _nValidezMaxima = 90;
84 
88  public static bool _Requerir_Especiales
89  {
90  get { return _lRequerir_Especiales; }
91  }
92  private static bool _lRequerir_Especiales = false;
93 
97  public static bool _Requerir_Numero
98  {
99  get { return _lRequerir_Numero; }
100  }
101  private static bool _lRequerir_Numero = false;
102 
106  public static bool _Requerir_Mayuscula
107  {
108  get { return _lRequerir_Mayuscula; }
109  }
110  private static bool _lRequerir_Mayuscula = Convert.ToBoolean(EW_GLOBAL._GetVariable("WL_STRONGPWD", false));
111 
117  public static FortalezaPassword _CheckPasswordStrenght(string tcPassword)
118  {
119  //Las validacones que utilizan Regex són validas
120 
121  int lnPuntuacion = 1;
122 
123  if (tcPassword.Length < 1)
124  return FortalezaPassword.Blanco;
125  if (tcPassword.Length < 4)
126  return FortalezaPassword.MuyDebil;
127 
128  //Mayor de 6
129  if (tcPassword.Length >= 6) lnPuntuacion++;
130 
131  //Mayor de 12
132  if (tcPassword.Length >= 12) lnPuntuacion++;
133 
134  // Carácteres especiales
135  if (!(tcPassword.All(c => char.IsLetter(c) || char.IsDigit(c)))) lnPuntuacion++;
136  //if (Regex.Match(tcPassword, @"/.[!,@,#,$,%,^,&,*,?,_,~,-,£,(,)]/", RegexOptions.ECMAScript).Success) lnPuntuacion++;
137 
138  // Carácteres numéricos
139  if (tcPassword.Any(c => char.IsDigit(c))) lnPuntuacion++;
140  //if (Regex.Match(tcPassword, @"/\d+/", RegexOptions.ECMAScript).Success) lnPuntuacion++;
141 
142  // Mayúsculas y minúsculas
143  if (tcPassword.Any(c => char.IsUpper(c)) && tcPassword.Any(c => char.IsLower(c))) lnPuntuacion++;
144  //if (Regex.Match(tcPassword, @"/[a-z]/", RegexOptions.ECMAScript).Success &&
145  // Regex.Match(tcPassword, @"/[A-Z]/", RegexOptions.ECMAScript).Success)
146  // lnPuntuacion++;
147 
148  //5 es el máximo permitido
149  if (lnPuntuacion > 5)
150  lnPuntuacion = 5;
151 
152  return (FortalezaPassword)lnPuntuacion;
153  }
154 
163  public static void _GetPasswordStrenght(string tcPassword, out int tcValor, out string tcDescripcion, out Color toColor)
164  {
165  Password.FortalezaPassword leFortaleza = Password._CheckPasswordStrenght(tcPassword);
166  switch (leFortaleza)
167  {
168  case Password.FortalezaPassword.Blanco:
169  tcValor = 0;
170  tcDescripcion = "";
171  toColor = Color.White;
172  break;
173  case Password.FortalezaPassword.MuyDebil:
174  tcValor = 20;
175  tcDescripcion = "Muy débil";
176  toColor = Color.Red;
177  break;
178  case Password.FortalezaPassword.Debil:
179  tcValor = 40;
180  tcDescripcion = "Débil";
181  toColor = Color.OrangeRed;
182  break;
183  case Password.FortalezaPassword.Media:
184  tcValor = 60;
185  tcDescripcion = "Media";
186  toColor = Color.Orange;
187  break;
188  case Password.FortalezaPassword.Fuerte:
189  tcValor = 80;
190  tcDescripcion = "Fuerte";
191  toColor = Color.LightGreen;
192  break;
193  case Password.FortalezaPassword.MuyFuerte:
194  tcValor = 100;
195  tcDescripcion = "Muy fuerte";
196  toColor = Color.Green;
197  break;
198  default:
199  tcValor = 0;
200  tcDescripcion = "Indeterminado";
201  toColor = Color.White;
202  break;
203  }
204  }
205  }
206 }