Python/other/password_generator.py
Pronoy Mandal 18ffc4dec8
Update password_generator.py (#7745)
* 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

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2022-10-29 14:54:13 +02:00

80 lines
2.3 KiB
Python

"""Password Generator allows you to generate a random password of length N."""
import secrets
from random import shuffle
from string import ascii_letters, digits, punctuation
def password_generator(length: int = 8) -> str:
"""
>>> 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 random_number(chars_incl, i):
pass # Put your code here...
def random_letters(chars_incl, i):
pass # Put your code here...
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())
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 passsword, You better save it.]")
if __name__ == "__main__":
main()