Skip to content

Update check_strong_password.py #7747

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 61 additions & 20 deletions other/check_strong_password.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,81 @@
from string import ascii_lowercase, ascii_uppercase, digits, punctuation


def strong_password_detector(password: str, min_length: int = 8) -> str:
def is_strong_password(password: str, min_length: int = 8) -> bool:
"""
>>> strong_password_detector('Hwea7$2!')
'This is a strong Password'
>>> is_strong_password('Hwea7$2!')
True

>>> strong_password_detector('Sh0r1')
'Your Password must be at least 8 characters long'
>>> is_strong_password('Sh0r1')
False

>>> strong_password_detector('Hello123')
'Password should contain UPPERCASE, lowercase, numbers, special characters'
>>> is_strong_password('Hello123')
False

>>> strong_password_detector('Hello1238udfhiaf038fajdvjjf!jaiuFhkqi1')
'This is a strong Password'
>>> is_strong_password('Hello1238udfhiaf038fajdvjjf!jaiuFhkqi1')
True

>>> strong_password_detector(0)
'Your Password must be at least 8 characters long'
>>> is_strong_password(0)
False
"""

if len(str(password)) < 8:
return "Your Password must be at least 8 characters long"
# Rudimentary check when an int or float type is used as a password
# having no. of digits less than min_length
if len(str(password)) < min_length:
return False

# Boolean character flags which are set on encountering a character of
# a given type
upper = any(char in ascii_uppercase for char in password)
lower = any(char in ascii_lowercase for char in password)
num = any(char in digits for char in password)
spec_char = any(char in punctuation for char in password)

if upper and lower and num and spec_char:
return "This is a strong Password"
return upper and lower and num and spec_char

else:
return (
"Password should contain UPPERCASE, lowercase, "
"numbers, special characters"
)

def is_strong_password_2(password: str, min_length: int = 8) -> bool:
"""
>>> is_strong_password_2('Hwea7$2!')
True

>>> is_strong_password_2('Sh0r1')
False

>>> is_strong_password_2('Hello123')
False

>>> is_strong_password_2('Hello1238udfhiaf038fajdvjjf!jaiuFhkqi1')
True

>>> is_strong_password_2(0)
False
"""
# Initialise boolean flags which toggle when the first occurrence of a
# type is encountered
upper = lower = num = spec_char = False

# Rudimentary check when an int or float type is used as a password
# having no. of digits less than min_length
if len(str(password)) < min_length:
return False

# Parse each of the characters and check whether they are within the
# concerned lexicographic boundaries
# Toggle respective flags when you encounter a character type
# Avoid the check when the concerned character flag is already set

for char in password:
if not num and "0" <= char <= "9":
num = True
elif not upper and "A" <= char <= "Z":
upper = True
elif not lower and "a" <= char <= "z":
lower = True
elif not spec_char and char in punctuation:
spec_char = True

return num and spec_char and upper and lower


if __name__ == "__main__":
Expand Down