-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandpwd
executable file
·32 lines (25 loc) · 1.3 KB
/
randpwd
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
#!/usr/bin/env python
import secrets, string, argparse
def parse_args():
parser = argparse.ArgumentParser(
description='Create a random password using a secure RNG.',
formatter_class=argparse.ArgumentDefaultsHelpFormatter
)
parser.add_argument('--nchars', type=int, default=16,
help='Number of characters to use for the password. The minimum is 14.')
parser.add_argument('--special_chars', type=str, default='',
help='A string of special characters to add to the alphabet. Put single quotes around it in case of any issues.')
parser.add_argument('--include_all_chars', action='store_true',
help='Include even characters like 1, l, I, L, o, O and 0 that are easily confused with each other in the alphabet.')
args = parser.parse_args()
if args.nchars < 14:
print('Using fewer than 14 characters will result in an insecure password.')
exit()
a = string.ascii_letters + string.digits + args.special_chars
if not args.include_all_chars:
for c in '1lILoO0':
a = a.replace(c, '')
return a, args.nchars
if __name__ == '__main__':
alphabet, nchars = parse_args()
print(''.join(secrets.choice(alphabet) for _ in range(nchars)))