forked from ikkebr/pyxkcdpass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pyxkcdpass.py
60 lines (39 loc) · 1.75 KB
/
pyxkcdpass.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# xkcdpass
# http://danielmcgraw.github.io/xkcdpass/
#
# pyxkcdpass
# https://github.com/ikkebr/xkcdpass
#
# Generate passwords like Randall Munroe (http://xkcd.com/936/)
# Default dictionary from http://www.englishclub.com/vocabulary/common-words-5000.htm
#
import os
import random
from optparse import OptionParser
class XKCDPass(object):
def __init__(self, dictionary='dictionary', length=4, *args, **kwargs):
self.dictionary = dictionary
self.length = length
def generate_random_password(self, length=None):
with open(self.dictionary, 'r') as dictionary:
return " ".join( map(str.strip, random.sample(dictionary.readlines(), length or self.length)) )
def main():
usage = "pyxkcdpass.py [-h|--help] [-l|--length=<length>] [-d|--dictionary=<path>]"
parser = OptionParser(usage=usage)
default_dictionary = os.path.join(os.path.dirname(__file__), 'dictionary')
parser.add_option("-d", "--dictionary", dest="dictionary_path",
help="Specify a path to a dictionary", metavar="PATH", default=default_dictionary)
parser.add_option("-l", "--length", dest="password_length", type="int",
help="Specify the password length", metavar="LENGTH", default=4)
(options, args) = parser.parse_args()
if options.password_length < 1:
parser.error("Your password should contain at least 1 word.")
if not os.path.isfile(options.dictionary_path):
parser.error("I cannot find your dictionary file. Please make sure it is readable.")
xkcdpass = XKCDPass(options.dictionary_path, options.password_length)
print(xkcdpass.generate_random_password())
if __name__ == "__main__":
main()