Meine Kriterien für die Passwortsicherheit sind wie folgt:
([email protected]#$&*)
(0-9)
Kann mir jemand bitte Regex für das gleiche geben. Alle Bedingungen müssen per Passwort erfüllt werden.
Sie können diese Überprüfungen mit positiven Look-Ahead-Aussagen durchführen
^(?=.*[A-Z].*[A-Z])(?=.*[[email protected]#$&*])(?=.*[0-9].*[0-9])(?=.*[a-z].*[a-z].*[a-z]).{8}$
Erläuterung:
^ Start anchor
(?=.*[A-Z].*[A-Z]) Ensure string has two uppercase letters.
(?=.*[[email protected]#$&*]) Ensure string has one special case letter.
(?=.*[0-9].*[0-9]) Ensure string has two digits.
(?=.*[a-z].*[a-z].*[a-z]) Ensure string has three lowercase letters.
.{8} Ensure string is of length 8.
$ End anchor.
Sie können positive Look-Aheads mit einer Länge von null verwenden, um jede Ihrer Einschränkungen separat festzulegen:
(?=.{8,})(?=.*\p{Lu}.*\p{Lu})(?=.*[[email protected]#$&*])(?=.*[0-9])(?=.*\p{Ll}.*\p{Ll})
Wenn Ihre Regex-Engine die \p
-Notation nicht unterstützt und pure ASCII ausreicht, können Sie \p{Lu}
durch [A-Z]
und \p{Ll}
durch [a-z]
ersetzen.
Die oben gegebenen Antworten sind perfekt, aber ich Suggest, um mehrere kleinere Regex zu verwenden, anstatt einen großen.
Das Aufteilen der langen Regex hat einige Vorteile:
Im Allgemeinen hält dieser Ansatz Code leicht zu pflegen .
Trotzdem teile ich einen Code, den ich in Swift schreibe, zum Beispiel:
struct RegExp {
/**
Check password complexity
- parameter password: password to test
- parameter length: password min length
- parameter patternsToEscape: patterns that password must not contains
- parameter caseSensitivty: specify if password must conforms case sensitivity or not
- parameter numericDigits: specify if password must conforms contains numeric digits or not
- returns: boolean that describes if password is valid or not
*/
static func checkPasswordComplexity(password password: String, length: Int, patternsToEscape: [String], caseSensitivty: Bool, numericDigits: Bool) -> Bool {
if (password.length < length) {
return false
}
if caseSensitivty {
let hasUpperCase = RegExp.matchesForRegexInText("[A-Z]", text: password).count > 0
if !hasUpperCase {
return false
}
let hasLowerCase = RegExp.matchesForRegexInText("[a-z]", text: password).count > 0
if !hasLowerCase {
return false
}
}
if numericDigits {
let hasNumbers = RegExp.matchesForRegexInText("\\d", text: password).count > 0
if !hasNumbers {
return false
}
}
if patternsToEscape.count > 0 {
let passwordLowerCase = password.lowercaseString
for pattern in patternsToEscape {
let hasMatchesWithPattern = RegExp.matchesForRegexInText(pattern, text: passwordLowerCase).count > 0
if hasMatchesWithPattern {
return false
}
}
}
return true
}
static func matchesForRegexInText(regex: String, text: String) -> [String] {
do {
let regex = try NSRegularExpression(pattern: regex, options: [])
let nsString = text as NSString
let results = regex.matchesInString(text,
options: [], range: NSMakeRange(0, nsString.length))
return results.map { nsString.substringWithRange($0.range)}
} catch let error as NSError {
print("invalid regex: \(error.localizedDescription)")
return []
}
}
}
Ich würde vorschlagen, hinzuzufügen
(?!.*pass|.*Word|.*1234|.*qwer|.*asdf) exclude common passwords
die Lösung von codaddict funktioniert gut, aber diese ist etwas effizienter: (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.
(?=[^[email protected]#$&*]*[[email protected]#$&*]) # 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)
Die negierten Zeichenklassen verbrauchen in einem einzigen Schritt alles bis zum gewünschten Zeichen, so dass kein Backtracking erforderlich ist. (Die Dot-Star-Lösung funktioniert einwandfrei, erfordert jedoch ein wenig Rückverfolgung.) Bei kurzen Zielzeichenfolgen wie Passwörtern ist diese Effizienzverbesserung vernachlässigbar.
Eine andere Lösung:
import re
passwordRegex = re.compile(r'''(
^(?=.*[A-Z].*[A-Z]) # at least two capital letters
(?=.*[[email protected]#$&*]) # 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()
Für PHP funktioniert das gut!
if(preg_match("/^(?=(?:[^A-Z]*[A-Z]){2})(?=(?:[^0-9]*[0-9]){2}).{8,}$/",
'CaSu4Li8')){
return true;
}else{
return fasle;
}
in diesem Fall ist das Ergebnis wahr
Thsks für @ridgerunner
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))
Das Passwort muss mindestens 3 der 4 folgenden Komplexitätsregeln erfüllen.
[mindestens 1 Großbuchstabe (AZ) mindestens 1 Kleinbuchstabe (az) mindestens 1 Ziffer (0-9) mindestens 1 Sonderzeichen - vergessen Sie nicht, Leerzeichen als Sonderzeichen zu behandeln auch]
mindestens 10 Zeichen
höchstens 128 Zeichen
nicht mehr als 2 identische Zeichen in einer Reihe (z. B. 111 nicht zulässig)
'^ (?.. (.)\1 {2}) ((? =. [az]) (? =. [AZ]) (? =. [0- 9]) | (? =. [az]) (? =. [AZ]) (? =. [^ a-zA-Z0-9]) | (? =. [AZ ]) (? =. [0-9]) (? =. [^ a-zA-Z0-9]) | (? =. [az]) (? =. [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}