-
Notifications
You must be signed in to change notification settings - Fork 1
/
make-password.py
executable file
·90 lines (75 loc) · 2.78 KB
/
make-password.py
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
#!/usr/bin/env python
#
# make-password.py - generate a human-readable password
# Inspired by "correct horse battery staple"
# (ref http://xkcd.com/936/)
#
# Copyright (C) 2014 Michael Davies <[email protected]>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
# 02111-1307, USA.
#
from __future__ import print_function
import os
import random
import sys
def find_words(num_words, max_length):
# Get a bunch of words from the system
f = open('/usr/share/dict/words')
words = [x.strip() for x in f.readlines()]
f.close()
# Only include words that aren't too long
# since my memory isn't that good :)
candidates = []
for word in words:
if len(word) <= max_length:
candidates.append(word)
chosen = []
# Choose the number of words required
for i in range(num_words):
chosen.append(random.choice(candidates))
return chosen
def generate_password(num_words, max_word_len, max_digits):
# Generate a password consisting of words separated by hyphens
password = '-'.join(find_words(num_words, max_word_len)).capitalize()
# Remove pesky aprostrophes
password = password.replace("'", "")
# Add a random number to the end
password += str(random.randint(1, max_digits))
return password
def _print_usage_and_exit(code):
msg = ("Usage: %s [number-of-passwords-to-be-generated]" %
os.path.basename(sys.argv[0]))
print(msg, file=sys.stderr)
sys.exit(code)
if __name__ == '__main__':
# Default number of passwords to generate
num_passwds = 10
# Default number of words to include in password
num_words = 3
# Default word length to include in password
word_length = 7
# Default maximum integer to append to the end
max_number = 999
# Allow number of passwords to be generated to be overwritten
if len(sys.argv) == 2:
try:
num_passwds = int(sys.argv[1])
except Exception as e:
_print_usage_and_exit(1)
elif len(sys.argv) > 2:
_print_usage_and_exit(2)
for i in range(num_passwds):
print(generate_password(num_words, word_length, max_number))