Skip to content

Added check_strong_password.py #4950

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

Merged
merged 6 commits into from
Oct 20, 2021
Merged
Changes from 3 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
47 changes: 47 additions & 0 deletions other/check_strong_password.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# This Will Check Whether A Given Password Is Strong Or Not
# It Follows The Rule that Length Of Password Should Be At Least 8 Characters
# And At Least 1 Lower, 1 Upper, 1 Number And 1 Special Character


import re


def strong_password_detector(password: str) -> str:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def strong_password_detector(password: str) -> str:
def strong_password_detector(password: str, min_length: int = 8) -> str:

and make appropriate changes in the function.

"""
>>> strong_password_detector('Hwea7$2!')
'This is a strong Password'

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

>>> strong_password_detector('Hello123')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One of the tests should be a long (like 20 char) string. Also tests for strong_password_detector(0), strong_password_detector(1.3), strong_password_detector(['H', 'w', 'e', 'a', '7', '$', '2', '!'])

'Password should contain UPPERCASE, lowercase, numbers, special characters'
"""

upper = re.compile(r"[A-Z]")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lower = re.compile(r"[a-z]")
num = re.compile(r"[0-9]")
spec_char = re.compile(r"[!@#$\^&\*\(\):;\'\"<>,\.\?\/|]")

if re.compile(r"\s").search(password) or len(password) < 8:
return "Your Password must be at least 8 characters long"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move these lines before line 22 so that we do not do the work on lines 22-24 if the password is too short.


elif (
upper.search(password)
and lower.search(password)
and num.search(password)
and spec_char.search(password)
):
return "This is a strong Password"

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


if __name__ == "__main__":
import doctest

doctest.testmod()