检查字符串足够强的密码 [英] Checking strings for a strong enough password

查看:104
本文介绍了检查字符串足够强的密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
强密码的正则表达式结果
需要的正则表达式密码强度<? / A>

Possible Duplicate:
Strong password regex
Need RegEx for password strength?

我只是想知道什么是最好的方式来搜索特定条件的字符串( 。密码强度是具体的),可以完成

I was just wondering what the best way to search a string for certain criteria (password strength to be specific) could be accomplished.

到目前为止,我有一个简单的:

So far I have a simple:

if(password.Length <= 7)
    {
        errorMessage = "Your password must be at least 8 characters.";
    }



我想能够检查大写字母,但我不知道什么方法或程序。我曾尝试谷歌搜索,搜索网站: http://msdn.microsoft.com 和搜索我的C#本书指数(C#编程3E中,芭芭拉·多伊尔),但我似乎无法找到任何

I would like to be able to check for capital letters, but I am not sure what the method or procedure is. I have tried Googling, searching the website: http://msdn.microsoft.com, and searching the index of my C# book (C# Programming 3E, by Barbara Doyle), but I can't seem to find any.

我知道我可以试试这个...:

I know I could try this...:

foreach(char c in password)
    {
        if(c!='A' || c!='B' || c!='C' || c!='D' ..... || c!='Z')
        {
            errorMessage = "Your password must contain at least one capital letter";
        }
    }



...但是,这将是非常草率,并必须加倍检查至少一个小写字母。我相信有一个更好的办法做到这一点,还是因为我上面的方法,至少简写。

...But that would be extremely sloppy, and would have to be doubled to check for at least one lowercase letter. I am sure there is a better way to do this, or at least shorthand for the method I have shown above.

另外,我可以决定检查特殊密码字符(似乎更容易在这个例子做比上面用大写和小写字母,所以我可能只使用特殊字符,我决定让他们必要的)。如果有一个简单的(或正确)的方式来做到这一点,我很想有知识,以及

Also, I may decide to check the password for special characters (seems easier to do in the example above than with upper and lower case letters, so I may just use that for special characters, should I decide to make them necessary). If there is an easy (or proper) way to do that, I would love to have that knowledge, as well.

无论如何,非常感谢你的任何帮助的人。可以给

Anyway, thank you so much for any help anyone can give.

推荐答案

我不能居功,因为我偷了这个从的这里

I can't take the credit, as I stole this from here

using System.Text;
using System.Text.RegularExpressions;

  public enum PasswordScore
  {
    Blank = 0,
    VeryWeak = 1,
    Weak = 2,
    Medium = 3,
    Strong = 4,
    VeryStrong = 5
  }

  public class PasswordAdvisor
  {
    public static PasswordScore CheckStrength(string password)
    {
      int score = 0;

      if (password.Length < 1)
        return PasswordScore.Blank;
      if (password.Length < 4)
        return PasswordScore.VeryWeak;

      if (password.Length >= 8)
        score++;
      if (password.Length >= 12)
        score++;
      if (Regex.Match(password, @"/\d+/", RegexOptions.ECMAScript).Success)
        score++;
      if (Regex.Match(password, @"/[a-z]/", RegexOptions.ECMAScript).Success &&
        Regex.Match(password, @"/[A-Z]/", RegexOptions.ECMAScript).Success)
        score++;
      if (Regex.Match(password, @"/.[!,@,#,$,%,^,&,*,?,_,~,-,£,(,)]/", RegexOptions.ECMAScript).Success)
        score++;

      return (PasswordScore)score;
    }
  }

请注意使用正则表达式用于检查大写字母。
这似乎是一个不错的方法,因为它会检查的长度,使用大写和小写字符,位数字和特殊字符。

Note the use of regex for checking for upper case characters. This appears to be a decent approach, as it checks length, use of upper and lower case characters, numeric digits and special characters.

** *更新*

我知道现在的问题是封闭的,但我可以添加更多的解释VoidKing了解一些概念。

I know the question is now closed, but I can add more explanation for VoidKing to understand some of the concepts.

一个PasswordScore从CheckStrength方法,它可以被用作在你的代码下一步做什么状态回来了。

A PasswordScore is returned from the CheckStrength method, which can be used as the condition for what to do next in your code.

下面是如何一个未经测试的演示上面的代码可以使用:

Here's an untested demo of how the above code could be used:

String password = "MyDummy_Password"; // Substitute with the user input string
PasswordScore passwordStrengthScore = PasswordAdvisor.CheckStrength(password);

switch (passwordStrengthScore) {
    case PasswordScore.Blank:
    case PasswordScore.VeryWeak:
    case PasswordScore.Weak:
            // Show an error message to the user
            break;
    case PasswordScore.Medium:
    case PasswordScore.Strong:
    case PasswordScore.VeryStrong:
           // Password deemed strong enough, allow user to be added to database etc
           break;
}



枚举在这种情况下,作为密码的强度进行分类的装置为人类可读基。保持代码干净,而且很明显的是什么代码怎么回事。

Enums are used in this case as a means of classifying the strength of the password into human-readable groups. Keeps the code clean, and makes it obvious what is going on in the code.

关于使用正则表达式的,如果你不熟悉他们的概念以及如何以及何时使用它们,我建议做一些研究,因为这些可以用于检查字符串中的模式是在许多不同的情况下非常有用。也许开始这里

Regarding the use of Regex's, if you're unfamiliar with the concept of them and how and when to use them, I suggest doing some research as these can be useful in many different scenarios for checking for patterns in strings. Perhaps start here.

这篇关于检查字符串足够强的密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆