-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpassword.py
More file actions
executable file
·98 lines (80 loc) · 2.57 KB
/
password.py
File metadata and controls
executable file
·98 lines (80 loc) · 2.57 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/home/nnnn/main/scripts/venv/bin/python3
## By Davoud Arsalani
## https://github.com/davoudarsalani/scripts
## https://github.com/davoudarsalani/scripts/blob/master/password.py
## https://raw.githubusercontent.com/davoudarsalani/scripts/master/password.py
## https://davoudarsalani.ir
import string
from getopt import getopt
from os import path
from random import sample
from re import sub
from subprocess import run
from sys import argv
from utils import Color, invalid, pipe_to_fzf
title = path.basename(__file__).replace('.py', '')
script_args = argv[1:]
Col = Color()
def display_help() -> None:
run('clear', shell=True)
print(f'''{Col.heading(f'{title}')} {Col.yellow('help')}
{Col.flag('-l|--length=')}{Col.default('[30]')}
{Col.flag('-c|--count=')}{Col.default('[5]')}''')
exit()
def getopts() -> None:
global length, count
try:
duos, duos_long = getopt(script_args, 'hl:c:', ['help', 'length=', 'count='])
except Exception as exc:
invalid(f'{exc!r}')
for opt, arg in duos:
if opt in ('-h', '--help'):
display_help()
elif opt in ('-l', '--length'):
length = int(arg)
elif opt in ('-c', '--count'):
count = int(arg)
def prompt(*args: list[str]) -> None:
global length, count
for arg in args:
if arg == '-l':
try:
length
except:
length = 30
elif arg == '-c':
try:
count
except:
count = 5
def generate(uppercase: bool=True, lowercase: bool=True, digits: bool=True, symbols: bool=True) -> None:
letters = ''
if uppercase:
letters += string.ascii_uppercase
if lowercase:
letters += string.ascii_lowercase
if digits:
letters += string.digits
if symbols:
puncs = string.punctuation
puncs = sub(r'[&/\\]', r'', puncs) ## removing &, / and \\ to prevent possible shell errors
letters += puncs
global length
if length > len(letters):
print(Col.yellow(f'Length exceeded maximumm number.\nLength is {len(letters)} now.'))
length = len(letters)
for x in range(count):
password = ''.join(sample(letters, length))
print(password)
getopts()
print(Col.heading(title))
main_items = ['password', 'help']
main_item = pipe_to_fzf(main_items)
if main_item == 'password':
prompt('-l', '-c')
print('all characters (&, / and \\ excluded):')
generate()
print('\nno symbols:')
generate(symbols=False)
elif main_item == 'help':
display_help()