Skip to content

Commit 18ffc4d

Browse files
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>
1 parent b0f68a0 commit 18ffc4d

File tree

1 file changed

+21
-19
lines changed

1 file changed

+21
-19
lines changed

other/password_generator.py

+21-19
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
"""Password Generator allows you to generate a random password of length N."""
2-
from random import choice, shuffle
2+
import secrets
3+
from random import shuffle
34
from string import ascii_letters, digits, punctuation
45

56

6-
def password_generator(length=8):
7+
def password_generator(length: int = 8) -> str:
78
"""
89
>>> len(password_generator())
910
8
@@ -17,58 +18,59 @@ def password_generator(length=8):
1718
0
1819
"""
1920
chars = ascii_letters + digits + punctuation
20-
return "".join(choice(chars) for x in range(length))
21+
return "".join(secrets.choice(chars) for _ in range(length))
2122

2223

2324
# ALTERNATIVE METHODS
24-
# ctbi= characters that must be in password
25+
# chars_incl= characters that must be in password
2526
# i= how many letters or characters the password length will be
26-
def alternative_password_generator(ctbi, i):
27+
def alternative_password_generator(chars_incl: str, i: int) -> str:
2728
# Password Generator = full boot with random_number, random_letters, and
2829
# random_character FUNCTIONS
2930
# Put your code here...
30-
i = i - len(ctbi)
31-
quotient = int(i / 3)
31+
i -= len(chars_incl)
32+
quotient = i // 3
3233
remainder = i % 3
33-
# chars = ctbi + random_letters(ascii_letters, i / 3 + remainder) +
34+
# chars = chars_incl + random_letters(ascii_letters, i / 3 + remainder) +
3435
# random_number(digits, i / 3) + random_characters(punctuation, i / 3)
3536
chars = (
36-
ctbi
37+
chars_incl
3738
+ random(ascii_letters, quotient + remainder)
3839
+ random(digits, quotient)
3940
+ random(punctuation, quotient)
4041
)
41-
chars = list(chars)
42-
shuffle(chars)
43-
return "".join(chars)
42+
list_of_chars = list(chars)
43+
shuffle(list_of_chars)
44+
return "".join(list_of_chars)
4445

4546
# random is a generalised function for letters, characters and numbers
4647

4748

48-
def random(ctbi, i):
49-
return "".join(choice(ctbi) for x in range(i))
49+
def random(chars_incl: str, i: int) -> str:
50+
return "".join(secrets.choice(chars_incl) for _ in range(i))
5051

5152

52-
def random_number(ctbi, i):
53+
def random_number(chars_incl, i):
5354
pass # Put your code here...
5455

5556

56-
def random_letters(ctbi, i):
57+
def random_letters(chars_incl, i):
5758
pass # Put your code here...
5859

5960

60-
def random_characters(ctbi, i):
61+
def random_characters(chars_incl, i):
6162
pass # Put your code here...
6263

6364

6465
def main():
6566
length = int(input("Please indicate the max length of your password: ").strip())
66-
ctbi = input(
67+
chars_incl = input(
6768
"Please indicate the characters that must be in your password: "
6869
).strip()
6970
print("Password generated:", password_generator(length))
7071
print(
71-
"Alternative Password generated:", alternative_password_generator(ctbi, length)
72+
"Alternative Password generated:",
73+
alternative_password_generator(chars_incl, length),
7274
)
7375
print("[If you are thinking of using this passsword, You better save it.]")
7476

0 commit comments

Comments
 (0)