From fe741fb3920c5ee568ced5fcbee9c1bc2b9e5e9c Mon Sep 17 00:00:00 2001 From: Pronoy Mandal Date: Thu, 27 Oct 2022 22:00:42 +0530 Subject: [PATCH 1/2] Update password_generator.py 1. Use secrets module instead of random for passwords as it gives a secure source of randomness 2. Add type annotations for functions 3. Replace ctbi (variable for the characters to be included) with a more meaningful and short name 4. Use integer division instead of obtaining the integer part of a division computing a floating point --- other/password_generator.py | 43 ++++++++++++++++++++----------------- 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/other/password_generator.py b/other/password_generator.py index c09afd7e6125..d29c91c1c88b 100644 --- a/other/password_generator.py +++ b/other/password_generator.py @@ -1,9 +1,10 @@ """Password Generator allows you to generate a random password of length N.""" -from random import choice, shuffle +import secrets +from random import shuffle from string import ascii_letters, digits, punctuation -def password_generator(length=8): +def password_generator(length: int = 8) -> str: """ >>> len(password_generator()) 8 @@ -17,58 +18,60 @@ def password_generator(length=8): 0 """ chars = ascii_letters + digits + punctuation - return "".join(choice(chars) for x in range(length)) + return "".join(secrets.choice(chars) for _ in range(length)) # ALTERNATIVE METHODS -# ctbi= characters that must be in password +# chars_incl= characters that must be in password # i= how many letters or characters the password length will be -def alternative_password_generator(ctbi, i): +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 = i - len(ctbi) - quotient = int(i / 3) + i -= len(chars_incl) + quotient = i // 3 remainder = i % 3 - # chars = ctbi + random_letters(ascii_letters, i / 3 + remainder) + + # chars = chars_incl + random_letters(ascii_letters, i / 3 + remainder) + # random_number(digits, i / 3) + random_characters(punctuation, i / 3) chars = ( - ctbi + chars_incl + random(ascii_letters, quotient + remainder) + random(digits, quotient) + random(punctuation, quotient) ) - chars = list(chars) - shuffle(chars) - return "".join(chars) + 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(ctbi, i): - return "".join(choice(ctbi) for x in range(i)) +def random(chars_incl: str, i: int) -> str: + return "".join(secrets.choice(chars_incl) for _ in range(i)) -def random_number(ctbi, i): +def random_number(chars_incl, i): pass # Put your code here... -def random_letters(ctbi, i): +def random_letters(chars_incl, i): pass # Put your code here... -def random_characters(ctbi, i): +def random_characters(chars_incl, i): pass # Put your code here... def main(): - length = int(input("Please indicate the max length of your password: ").strip()) - ctbi = input( + 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(ctbi, length) + "Alternative Password generated:", alternative_password_generator( + chars_incl, length) ) print("[If you are thinking of using this passsword, You better save it.]") From a06e35acb04d7ea553d09e9c7683ed43dca26079 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 27 Oct 2022 16:34:36 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- other/password_generator.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/other/password_generator.py b/other/password_generator.py index d29c91c1c88b..8f9d58a33b82 100644 --- a/other/password_generator.py +++ b/other/password_generator.py @@ -63,15 +63,14 @@ def random_characters(chars_incl, i): def main(): - length = int( - input("Please indicate the max length of your password: ").strip()) + 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) + "Alternative Password generated:", + alternative_password_generator(chars_incl, length), ) print("[If you are thinking of using this passsword, You better save it.]")