Password Checker But the password does not contain the username

Question:

I have a project, which is to check the password, and it must contain it :
length should be between 6-16 characters
Password contains at least one digit
Password contains at least one of the special characters ($@!*)
Password has at least one uppercase letter
Password has at least one lowercase letter

I should test that the password does not contain repeated pattern, names, known phrases If the password entered is combined with a username, your project should check the password does not contain characters from the username.
example :
Marwan408
Marwan@123456789
Error : The password must not contain the username

Marwan408
Ma@r@wan@123456789
Error : The password must not contain the username (Because the username has the first two letters of it next to each other)

Marwan408
M@a@r@w@a@n@123456789
True

Asked By: Pluto

||

Answers:

If I’m understanding this correctly, the password is not accepted if it contains any two consecutive characters (case sensitive) from the username, right? If that’s the case, you could split the username into a list of two-character units. Then check if any of those two-character units are found in the password. Here’s an example:

def get_char_combos(text):
    text_len = len(text)
    char_combos = []
    for char_i in range(text_len):
        if (char_i + 1 < text_len):
            char = text[char_i]
            next_char = text[char_i + 1]
            char_combos.append(char + next_char)
    return char_combos

username = 'Marwan408'
password = 'Ma@r@wan@123456789'
char_combos = get_char_combos(username)
password_ok = True
for char_combo in char_combos:
    if char_combo in password:
        password_ok = False
print(password_ok)

EDIT Here’s an update that utilizes some suggestions made by @SUTerliakov:

username = 'Marwan408'
password = 'Ma@r@wan@123456789'
char_combos = zip(username, username[1:])
match_in_password = any(''.join(char_combo) in password for char_combo in char_combos)
print(match_in_password)
Answered By: great_pan
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.