-
Notifications
You must be signed in to change notification settings - Fork 4
/
randpass
executable file
·188 lines (138 loc) · 5.51 KB
/
randpass
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#! /usr/bin/env python
import random
import os, sys
import string, itertools
from optparse import Option, OptionParser, OptionValueError
#
# Simple utility to generate random passwords of arbitrary length
#
# Author: Sudhi Herle <sudhi at herle.net>
# License: GPLv2
#
def randnum(n=4):
"""Return a 'n' decimal digit random number"""
l = 10 ** (n - 1)
#print "n=%d, %ld <= x < %ld" % (n, l, l *10)
v = random.randint(l, (l * 10) - 1)
return "%ld" % v
def randpass(sampleset, n):
"""Generate password n bytes long chosen from chars in
sampleset"""
random.shuffle(sampleset)
x = []
size = len(sampleset)
while n > 0:
if n > size:
s = size
else:
s = n
x += random.sample(sampleset, s)
n -= s
return ''.join(x)
def gen_words(words, nwords=3):
"""Generate random words from words and concatenate."""
random.shuffle(words)
v = random.sample(words, nwords)
return ' '.join(v)
def readwords(fname, wlen):
fd = open(fname)
z = []
for l in fd:
l = l.strip()
if len(l) > 1 and len(l) <= wlen:
z.append(l)
fd.close()
return z
def randcase(w):
"""Turn upto 4 letters into upper case"""
n = random.randint(2, 4)
chars = list(w)
l = len(chars)
while n > 0:
n -= 1
r = random.randint(0, l-1)
c = chars[r]
chars[r] = c.upper()
return ''.join(chars)
# Hermaan Schaaf's clever routine doesn't use a dictionary :-)
def wordlist(wlen):
initial_consonants = (set(string.ascii_lowercase) - set('aeiou')
# remove those easily confused with others
- set('qxc')
# add some crunchy clusters
| set(['bl', 'br', 'cl', 'cr', 'dr', 'fl',
'fr', 'gl', 'gr', 'pl', 'pr', 'sk',
'sl', 'sm', 'sn', 'sp', 'st', 'str',
'sw', 'tr'])
)
final_consonants = (set(string.ascii_lowercase) - set('aeiou')
# confusable
- set('qxcsj')
# crunchy clusters
| set(['ct', 'ft', 'mp', 'nd', 'ng', 'nk', 'nt',
'pt', 'sk', 'sp', 'ss', 'st'])
)
vowels = 'aeiou' # we'll keep this simple
# each syllable is consonant-vowel-consonant "pronounceable"
z = map(''.join, itertools.product(initial_consonants,
vowels,
final_consonants))
return [ x for x in z if len(x) > 1 and len(x) <= wlen ]
usage = """%s: Simple utility to generate random passwords.
Usage: %s [options] [count]""" % (sys.argv[0], sys.argv[0])
parser = OptionParser(usage)
parser.add_option("-d", "--dictionary", dest="wordlist", type='string',
default=None, metavar='F',
help="Read a wordlist from file 'F' [%default]")
parser.add_option("-0", "--print0", dest="print0", action="store_true",
default=False,
help="Use '\\0' as the separator between multiple passwords [%default]")
parser.add_option("-n", "--numeric", dest="numeric_only",
action="store_true", default=False,
help="Only use Numbers '0-9' [%default]")
parser.add_option("-a", "--alphanumeric", dest="alphanumeric",
action="store_true", default=False,
help="Only use Alphanumeric chars 'a-zA-Z0-9' [%default]")
parser.add_option("-l", "--length", dest="length", type='int',
default=9, metavar='L',
help="Limit word or password length to 'L' [%default]")
parser.add_option("-H", "--human-readable", dest="words", action="store_true",
default=False,
help="Generate human readable passwords [%default]")
parser.add_option("-L", "--lowercase", dest="lowercase", action="store_true",
default=False,
help="Use lowercase for human readable passwords [%default]")
parser.add_option("-s", "--separate", dest="spaceify", action="store_true",
default=False,
help="Use whitespace to delineate words in a password [%default]")
parser.add_option("-w", "--words", dest="nwords", type="int", action="store",
default=2, metavar="N",
help="Generate N words of human readable password [%default]")
parser.add_option("-C", "--capitalize", dest="capitalize", action="store_true",
default=False,
help="Capitalize first letter of a word [%default]")
opt, args = parser.parse_args()
n = int(args[0]) if len(args) > 0 else 1
if opt.words:
words = readwords(opt.wordlist, opt.length) if opt.wordlist else wordlist(opt.length)
def gen0():
random.shuffle(words)
z = random.sample(words, opt.nwords)
if opt.capitalize:
z = [ x.capitalize() for x in z ]
if opt.alphanumeric:
z.append(randnum(4))
sep = ' ' if opt.spaceify else ''
return sep.join(z)
gen = gen0
elif opt.numeric_only:
gen = lambda: randnum(opt.length)
else:
chars = list(string.letters + string.digits)
if not opt.alphanumeric:
chars += list(r"+_!~@#%/`'\"[]{}")
gen = lambda: randpass(chars, opt.length)
rr = ( gen() for a in range(n) )
ps = '\0' if opt.print0 else '\n'
print(ps.join(rr))
# EOF