Regex to validate password strength

后端 未结 11 741
天涯浪人
天涯浪人 2020-11-22 02:13

My password strength criteria is as below :

  • 8 characters length
  • 2 letters in Upper Case
  • 1 Special Character (!@#$&*)
  • <
相关标签:
11条回答
  • 2020-11-22 02:47

    Password must meet at least 3 out of the following 4 complexity rules,

    [at least 1 uppercase character (A-Z) at least 1 lowercase character (a-z) at least 1 digit (0-9) at least 1 special character — do not forget to treat space as special characters too]

    at least 10 characters

    at most 128 characters

    not more than 2 identical characters in a row (e.g., 111 not allowed)

    '^(?!.(.)\1{2}) ((?=.[a-z])(?=.[A-Z])(?=.[0-9])|(?=.[a-z])(?=.[A-Z])(?=.[^a-zA-Z0-9])|(?=.[A-Z])(?=.[0-9])(?=.[^a-zA-Z0-9])|(?=.[a-z])(?=.[0-9])(?=.*[^a-zA-Z0-9])).{10,127}$'

    (?!.*(.)\1{2})

    (?=.[a-z])(?=.[A-Z])(?=.*[0-9])

    (?=.[a-z])(?=.[A-Z])(?=.*[^a-zA-Z0-9])

    (?=.[A-Z])(?=.[0-9])(?=.*[^a-zA-Z0-9])

    (?=.[a-z])(?=.[0-9])(?=.*[^a-zA-Z0-9])

    .{10.127}

    0 讨论(0)
  • 2020-11-22 02:49

    All of above regex unfortunately didn't worked for me. A strong password's basic rules are

    • Should contain at least a capital letter
    • Should contain at least a small letter
    • Should contain at least a number
    • Should contain at least a special character
    • And minimum length

    So, Best Regex would be

    ^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!@#\$%\^&\*]).{8,}$
    

    The above regex have minimum length of 8. You can change it from {8,} to {any_number,}

    Modification in rules?

    let' say you want minimum x characters small letters, y characters capital letters, z characters numbers, Total minimum length w. Then try below regex

    ^(?=.*[a-z]{x,})(?=.*[A-Z]{y,})(?=.*[0-9]{z,})(?=.*[!@#\$%\^&\*]).{w,}$
    

    Note: Change x, y, z, w in regex

    Edit: Updated regex answer

    Edit2: Added modification

    0 讨论(0)
  • 2020-11-22 02:56

    You can use zero-length positive look-aheads to specify each of your constraints separately:

    (?=.{8,})(?=.*\p{Lu}.*\p{Lu})(?=.*[!@#$&*])(?=.*[0-9])(?=.*\p{Ll}.*\p{Ll})
    

    If your regex engine doesn't support the \p notation and pure ASCII is enough, then you can replace \p{Lu} with [A-Z] and \p{Ll} with [a-z].

    0 讨论(0)
  • 2020-11-22 02:57

    codaddict's solution works fine, but this one is a bit more efficient: (Python syntax)

    password = re.compile(r"""(?#!py password Rev:20160831_2100)
        # Validate password: 2 upper, 1 special, 2 digit, 1 lower, 8 chars.
        ^                        # Anchor to start of string.
        (?=(?:[^A-Z]*[A-Z]){2})  # At least two uppercase.
        (?=[^!@#$&*]*[!@#$&*])   # At least one "special".
        (?=(?:[^0-9]*[0-9]){2})  # At least two digit.
        .{8,}                    # Password length is 8 or more.
        $                        # Anchor to end of string.
        """, re.VERBOSE)
    

    The negated character classes consume everything up to the desired character in a single step, requiring zero backtracking. (The dot star solution works just fine, but does require some backtracking.) Of course with short target strings such as passwords, this efficiency improvement will be negligible.

    0 讨论(0)
  • 2020-11-22 02:57

    Another solution:

    import re
    
    passwordRegex = re.compile(r'''(
        ^(?=.*[A-Z].*[A-Z])                # at least two capital letters
        (?=.*[!@#$&*])                     # at least one of these special c-er
        (?=.*[0-9].*[0-9])                 # at least two numeric digits
        (?=.*[a-z].*[a-z].*[a-z])          # at least three lower case letters
        .{8,}                              # at least 8 total digits
        $
        )''', re.VERBOSE)
    
    def userInputPasswordCheck():
        print('Enter a potential password:')
        while True:
            m = input()
            mo = passwordRegex.search(m) 
            if (not mo):
               print('''
    Your password should have at least one special charachter,
    two digits, two uppercase and three lowercase charachter. Length: 8+ ch-ers.
    
    Enter another password:''')          
            else:
               print('Password is strong')
               return
    userInputPasswordCheck()
    
    0 讨论(0)
  • 2020-11-22 02:59
    import re
    
    RegexLength=re.compile(r'^\S{8,}$')
    RegexDigit=re.compile(r'\d')
    RegexLower=re.compile(r'[a-z]')
    RegexUpper=re.compile(r'[A-Z]')
    
    
    def IsStrongPW(password):
        if RegexLength.search(password) == None or RegexDigit.search(password) == None or RegexUpper.search(password) == None or RegexLower.search(password) == None:
            return False
        else:
            return True
    
    while True:
        userpw=input("please input your passord to check: \n")
        if userpw == "exit":
            break
        else:
            print(IsStrongPW(userpw))
    
    0 讨论(0)
提交回复
热议问题