mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-23 21:11:08 +00:00
a936e94704
* Enable ruff ARG001 rule * Fix dynamic_programming/combination_sum_iv.py * Fix machine_learning/frequent_pattern_growth.py * Fix other/davis_putnam_logemann_loveland.py * Fix other/password.py * Fix * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Fix physics/n_body_simulation.py * Fix project_euler/problem_145/sol1.py * Fix project_euler/problem_174/sol1.py * Fix scheduling/highest_response_ratio_next.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Fix * Fix * Fix scheduling/job_sequencing_with_deadline.py * Fix scheduling/job_sequencing_with_deadline.py * Fix * Fix --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
98 lines
3.0 KiB
Python
98 lines
3.0 KiB
Python
import secrets
|
|
from random import shuffle
|
|
from string import ascii_letters, ascii_lowercase, ascii_uppercase, digits, punctuation
|
|
|
|
|
|
def password_generator(length: int = 8) -> str:
|
|
"""
|
|
Password Generator allows you to generate a random password of length N.
|
|
|
|
>>> 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
|
|
"""
|
|
chars = ascii_letters + digits + punctuation
|
|
return "".join(secrets.choice(chars) for _ in range(length))
|
|
|
|
|
|
# ALTERNATIVE METHODS
|
|
# chars_incl= characters that must be in password
|
|
# i= how many letters or characters the password length will be
|
|
def alternative_password_generator(chars_incl: str, i: int) -> str:
|
|
# Password Generator = full boot with random_number, random_letters, and
|
|
# random_character FUNCTIONS
|
|
# Put your code here...
|
|
i -= len(chars_incl)
|
|
quotient = i // 3
|
|
remainder = i % 3
|
|
# chars = chars_incl + random_letters(ascii_letters, i / 3 + remainder) +
|
|
# random_number(digits, i / 3) + random_characters(punctuation, i / 3)
|
|
chars = (
|
|
chars_incl
|
|
+ random(ascii_letters, quotient + remainder)
|
|
+ random(digits, quotient)
|
|
+ random(punctuation, quotient)
|
|
)
|
|
list_of_chars = list(chars)
|
|
shuffle(list_of_chars)
|
|
return "".join(list_of_chars)
|
|
|
|
# random is a generalised function for letters, characters and numbers
|
|
|
|
|
|
def random(chars_incl: str, i: int) -> str:
|
|
return "".join(secrets.choice(chars_incl) for _ in range(i))
|
|
|
|
|
|
def is_strong_password(password: str, min_length: int = 8) -> bool:
|
|
"""
|
|
This will check whether a given password is strong or not. The password must be at
|
|
least as long as the provided minimum length, and it must contain at least 1
|
|
lowercase letter, 1 uppercase letter, 1 number and 1 special character.
|
|
|
|
>>> 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
|
|
"""
|
|
|
|
if len(password) < min_length:
|
|
return False
|
|
|
|
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)
|
|
|
|
return upper and lower and num and spec_char
|
|
|
|
|
|
def main():
|
|
length = int(input("Please indicate the max length of your password: ").strip())
|
|
chars_incl = input(
|
|
"Please indicate the characters that must be in your password: "
|
|
).strip()
|
|
print("Password generated:", password_generator(length))
|
|
print(
|
|
"Alternative Password generated:",
|
|
alternative_password_generator(chars_incl, length),
|
|
)
|
|
print("[If you are thinking of using this password, You better save it.]")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|