-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoken_generator.py
More file actions
48 lines (42 loc) · 1.27 KB
/
token_generator.py
File metadata and controls
48 lines (42 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import os
import sys
import secrets
import string
def generate_token(nbytes):
alphabet = string.ascii_letters + string.digits
while True:
token = secrets.token_urlsafe(nbytes)
if all(c in alphabet for c in token):
return token
def user_input():
while True:
try:
nbytes = int(input('Token length (from 8 - 64): '))
except ValueError:
print('\nError: Invalid input.\n')
continue
if nbytes < 8 or nbytes > 64:
print('\nError: Token length must be between 8 and 64.\n')
continue
else:
token = generate_token(nbytes=nbytes)
print(f'Generated token: {token} \n')
break
def main():
os.system('cls' if os.name == 'nt' else 'clear')
app_name = 'Token Generator'
print(f'{"-" * 48}')
print(f'{" " * 12}{app_name}{" " * 12}')
print(f'{"-" * 48}')
user_input()
while True:
response = input('Do you want to continue? (Y/N) ')
if response == 'y' or response == 'Y':
main()
elif response == 'n' or response == 'N':
sys.exit()
else:
print('\nError: Please select Y or N.\n')
continue
if __name__ == '__main__':
main()