2022-10-29 12:54:13 +00:00
|
|
|
import secrets
|
|
|
|
from random import shuffle
|
2022-11-02 16:20:57 +00:00
|
|
|
from string import ascii_letters, ascii_lowercase, ascii_uppercase, digits, punctuation
|
2019-07-07 15:17:38 +00:00
|
|
|
|
|
|
|
|
2022-10-29 12:54:13 +00:00
|
|
|
def password_generator(length: int = 8) -> str:
|
2019-07-07 15:17:38 +00:00
|
|
|
"""
|
2022-11-02 16:20:57 +00:00
|
|
|
Password Generator allows you to generate a random password of length N.
|
|
|
|
|
2019-07-07 15:17:38 +00:00
|
|
|
>>> len(password_generator())
|
|
|
|
8
|
|
|
|
>>> len(password_generator(length=16))
|
|
|
|
16
|
|
|
|
>>> len(password_generator(257))
|
|
|
|
257
|
|
|
|
>>> len(password_generator(length=0))
|
|
|
|
0
|
|
|
|
>>> len(password_generator(-1))
|
|
|
|
0
|
|
|
|
"""
|
2021-04-22 08:52:54 +00:00
|
|
|
chars = ascii_letters + digits + punctuation
|
2022-10-29 12:54:13 +00:00
|
|
|
return "".join(secrets.choice(chars) for _ in range(length))
|
2019-07-07 15:17:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
# ALTERNATIVE METHODS
|
2022-10-29 12:54:13 +00:00
|
|
|
# chars_incl= characters that must be in password
|
2019-07-07 15:17:38 +00:00
|
|
|
# i= how many letters or characters the password length will be
|
2022-10-29 12:54:13 +00:00
|
|
|
def alternative_password_generator(chars_incl: str, i: int) -> str:
|
2021-11-24 14:23:44 +00:00
|
|
|
# Password Generator = full boot with random_number, random_letters, and
|
2019-07-07 15:17:38 +00:00
|
|
|
# random_character FUNCTIONS
|
2019-10-21 20:05:12 +00:00
|
|
|
# Put your code here...
|
2022-10-29 12:54:13 +00:00
|
|
|
i -= len(chars_incl)
|
|
|
|
quotient = i // 3
|
2019-10-22 17:13:48 +00:00
|
|
|
remainder = i % 3
|
2022-10-29 12:54:13 +00:00
|
|
|
# chars = chars_incl + random_letters(ascii_letters, i / 3 + remainder) +
|
2020-05-01 21:36:35 +00:00
|
|
|
# random_number(digits, i / 3) + random_characters(punctuation, i / 3)
|
2019-10-22 17:13:48 +00:00
|
|
|
chars = (
|
2022-10-29 12:54:13 +00:00
|
|
|
chars_incl
|
2019-10-22 17:13:48 +00:00
|
|
|
+ random(ascii_letters, quotient + remainder)
|
|
|
|
+ random(digits, quotient)
|
|
|
|
+ random(punctuation, quotient)
|
|
|
|
)
|
2022-10-29 12:54:13 +00:00
|
|
|
list_of_chars = list(chars)
|
|
|
|
shuffle(list_of_chars)
|
|
|
|
return "".join(list_of_chars)
|
2019-10-22 17:13:48 +00:00
|
|
|
|
|
|
|
# random is a generalised function for letters, characters and numbers
|
|
|
|
|
|
|
|
|
2022-10-29 12:54:13 +00:00
|
|
|
def random(chars_incl: str, i: int) -> str:
|
|
|
|
return "".join(secrets.choice(chars_incl) for _ in range(i))
|
2019-10-22 17:13:48 +00:00
|
|
|
|
|
|
|
|
2022-10-29 12:54:13 +00:00
|
|
|
def random_number(chars_incl, i):
|
2019-07-07 15:17:38 +00:00
|
|
|
pass # Put your code here...
|
2018-01-06 06:54:10 +00:00
|
|
|
|
|
|
|
|
2022-10-29 12:54:13 +00:00
|
|
|
def random_letters(chars_incl, i):
|
2019-07-07 15:17:38 +00:00
|
|
|
pass # Put your code here...
|
2018-01-06 06:54:10 +00:00
|
|
|
|
|
|
|
|
2022-10-29 12:54:13 +00:00
|
|
|
def random_characters(chars_incl, i):
|
2019-07-07 15:17:38 +00:00
|
|
|
pass # Put your code here...
|
|
|
|
|
|
|
|
|
2022-11-02 16:20:57 +00:00
|
|
|
# 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
|
2022-11-06 14:54:44 +00:00
|
|
|
def is_strong_password(password: str, min_length: int = 8) -> bool:
|
2022-11-02 16:20:57 +00:00
|
|
|
"""
|
2022-11-06 14:54:44 +00:00
|
|
|
>>> is_strong_password('Hwea7$2!')
|
|
|
|
True
|
|
|
|
>>> is_strong_password('Sh0r1')
|
|
|
|
False
|
|
|
|
>>> is_strong_password('Hello123')
|
|
|
|
False
|
|
|
|
>>> is_strong_password('Hello1238udfhiaf038fajdvjjf!jaiuFhkqi1')
|
|
|
|
True
|
|
|
|
>>> is_strong_password('0')
|
|
|
|
False
|
2022-11-02 16:20:57 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
if len(password) < min_length:
|
2022-11-06 14:54:44 +00:00
|
|
|
# Your Password must be at least 8 characters long
|
|
|
|
return False
|
2022-11-02 16:20:57 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
2022-11-20 11:00:27 +00:00
|
|
|
return upper and lower and num and spec_char
|
|
|
|
# Passwords should contain UPPERCASE, lowerase
|
|
|
|
# numbers, and special characters
|
2022-11-02 16:20:57 +00:00
|
|
|
|
|
|
|
|
2019-07-07 15:17:38 +00:00
|
|
|
def main():
|
2019-10-05 05:14:13 +00:00
|
|
|
length = int(input("Please indicate the max length of your password: ").strip())
|
2022-10-29 12:54:13 +00:00
|
|
|
chars_incl = input(
|
2019-10-22 17:13:48 +00:00
|
|
|
"Please indicate the characters that must be in your password: "
|
|
|
|
).strip()
|
2019-10-05 05:14:13 +00:00
|
|
|
print("Password generated:", password_generator(length))
|
2019-10-22 17:13:48 +00:00
|
|
|
print(
|
2022-10-29 12:54:13 +00:00
|
|
|
"Alternative Password generated:",
|
|
|
|
alternative_password_generator(chars_incl, length),
|
2019-10-22 17:13:48 +00:00
|
|
|
)
|
2019-10-05 05:14:13 +00:00
|
|
|
print("[If you are thinking of using this passsword, You better save it.]")
|
2019-07-07 15:17:38 +00:00
|
|
|
|
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
if __name__ == "__main__":
|
2019-07-07 15:17:38 +00:00
|
|
|
main()
|