地铁二期项目正式开始

CheckPwd.cs 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. using System.Text.RegularExpressions;
  2. namespace YTSoft.Common
  3. {
  4. public class checkpwd
  5. {
  6. /// <summary>
  7. /// 计算密码强度
  8. /// </summary>
  9. /// <param name="password">密码字符串</param>
  10. /// <returns></returns>
  11. ///
  12. public class Chkrslt
  13. {
  14. public bool RSL { set; get; }
  15. public string MSG { set; get; }
  16. }
  17. public static Chkrslt PasswordStrength(string password)
  18. {
  19. if (password.Length < 8 || password.Length > 16)
  20. {
  21. return new Chkrslt() { RSL = false, MSG = "密码长度不符合,密码长度:8-16" };
  22. }
  23. Regex rgx = new Regex(@"^[0-9a-zA-Z\x21-\x7e]{8,16}$");
  24. if (!rgx.IsMatch(password))
  25. {
  26. return new Chkrslt() { RSL = false, MSG = "密码只能包含数字,字母和字符" };
  27. }
  28. //字符统计
  29. int iNum = 0, iLtt = 0, iSym = 0;
  30. foreach (char c in password)
  31. {
  32. if (c >= '0' && c <= '9') iNum++;
  33. else if (c >= 'a' && c <= 'z') iLtt++;
  34. else if (c >= 'A' && c <= 'Z') iLtt++;
  35. else iSym++;
  36. }
  37. if (iLtt == 0 && iSym == 0) return new Chkrslt() { RSL = false, MSG = "纯数字密码,请加入字符和字母" }; //纯数字密码
  38. if (iNum == 0 && iLtt == 0) return new Chkrslt() { RSL = false, MSG = "纯符号密码,请加入数字和字母" }; //纯符号密码
  39. if (iNum == 0 && iSym == 0) return new Chkrslt() { RSL = false, MSG = "纯字母密码,请加入字符和数字" }; //纯字母密码
  40. if (iLtt == 0) return new Chkrslt() { RSL = true, MSG = "数字和符号构成的密码,请加入字母" }; ; //数字和符号构成的密码
  41. if (iSym == 0) return new Chkrslt() { RSL = true, MSG = "数字和字母构成的密码,请加入字符" }; ; //数字和字母构成的密码
  42. if (iNum == 0) return new Chkrslt() { RSL = true, MSG = "字母和符号构成的密码,请加入数字" }; //字母和符号构成的密码
  43. return new Chkrslt() { RSL = true, MSG = "密码符合" };
  44. }
  45. }
  46. }