-
Notifications
You must be signed in to change notification settings - Fork 1
/
pwgen
executable file
·125 lines (106 loc) · 2.74 KB
/
pwgen
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# NAME
#
# pwgen - generate random passwords
#
# SYNOPSIS
#
# pwgen [-h] [-u] [-l] [-d] [-s] [LENGTH]
#
# OPTIONS
#
# Positional arguments
#
# LENGTH length of password (must be >= 4)
#
# Optional arguments
#
# -h, --help show this help message and exit
# -u, --upper suppress upper characters
# -l, --lower suppress lower characters
# -d, --digit suppress digits
# -s, --special suppress special characters
#
from __future__ import print_function
from random import SystemRandom
import argparse
LOWER = list("abcdefghjkmnpqrstuvwxyz")
UPPER = list("ABCDEFGHJKMNPQRSTUVWXYZ")
DIGIT = list("23456789")
SPECIAL = list("!#$%&*+-/:<=>?@\~")
def generate(length=32, lower=True, upper=True, digit=True, special=True):
random = SystemRandom()
passwd = list()
char_pool = list()
# Ensure that characters of each kind are present at least once.
if upper:
passwd.append(random.choice(UPPER))
char_pool.extend(UPPER)
if lower:
passwd.append(random.choice(LOWER))
char_pool.extend(LOWER)
if digit:
passwd.append(random.choice(DIGIT))
char_pool.extend(DIGIT)
if special:
passwd.append(random.choice(SPECIAL))
char_pool.extend(SPECIAL)
passwd.extend([random.choice(char_pool) for i in range(length - len(passwd))])
random.shuffle(passwd)
return "".join(passwd)
def passwd_length(length):
length = int(length)
if length < 4:
raise argparse.ArgumentTypeError("must be >= 4")
else:
return length
def main():
parser = argparse.ArgumentParser(
prog="pwgen", description="generate random passwords"
)
parser.add_argument(
"length",
type=passwd_length,
default=32,
metavar="LENGTH",
nargs="?",
help="length of password (must be >= 4)",
)
parser.add_argument(
"-u",
"--upper",
action="store_false",
default=True,
help="suppress upper characters",
)
parser.add_argument(
"-l",
"--lower",
action="store_false",
default=True,
help="suppress lower characters",
)
parser.add_argument(
"-d", "--digit", action="store_false", default=True, help="suppress digits"
)
parser.add_argument(
"-s",
"--special",
action="store_false",
default=True,
help="suppress special characters",
)
args = parser.parse_args()
print(
generate(
length=args.length,
upper=args.upper,
lower=args.lower,
digit=args.digit,
special=args.special,
)
)
if __name__ == "__main__":
main()