python : a simple password generator

Below is a simple password or passphrase generator that has been written in python. It will generate at least 5 iterations so that you can pick the best one for you. You can specify the max-length of the password (up to 80 characters).

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
#!/usr/bin/python
"""
A simple password generator written in Python
No responsibility is taken for this code. Use at your own risk
"""
 
import random
import os
import sys
from optparse import OptionParser
 
def genrandom(length, number):
    """
    function to generate a random string based on the letterset,
    numberset and special set variables. Remove Characters from
    the variables if you need to smaller character set in your
    passwords that get generated.
    """
    iterations = 0
    genstring = ''
    if length >= 80:
        length = 80
    letterset = r'ABCDEFGHIJKLMNOQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
    numberset = r'0123456789'
    specialset = r'!@#$%^&*\(\) '
    charset = letterset + numberset + specialset
    while (iterations < number ):
        iterations +=  1
        while ( len(genstring) < length ):
            genstring = genstring + ''.join(random.choice(charset))
            continue
        else:
            print ' ' + genstring
            genstring = ''
            continue
    else:
        print genstring
 
def usage():
    """
    help for the command line
    """
    print "Usage: python %s generate -m  -i " % (sys.argv[0])
 
def main():
    """
    Core Logic
    """
    parser = OptionParser(add_help_option=False)
    parser.add_option('-h', '--help', action='help')
    parser.add_option('-i', '--iterations', default=5, action='store',
        type='int', help='the number of passwords to generate.')
    parser.add_option('-m', '--max-length', default=10, action='store',
        type='int', help='the length of the passwords to be generated.')
    (options, args) = parser.parse_args()
 
    if len(args) < 1:
        usage()
        return os.EX_USAGE
 
    command = args[0]
    maxlength = options.max_length
    number = options.iterations
    print "Generating a random string"
    print ""
    genrandom(maxlength, number)
 
if __name__ == '__main__':
    sys.exit(main())

Running the above command can be as simple as

chmod 700 password-generator.py
./password-generator.py generate -m 16 -i 5

Changelog :

04/August/2013 – Original Release

03/October/2013 : Major Update

  • Include Iterations functionality. Defaults to 5 can generate thousands as there is no maximum number permitted.
  • pylint code to make more compliant and less hacky. Major tidy up.
  • Added docstrings support for better commenting.
  • changed the character set variable layout to allow for easier manipulation of permitted characters in the random string.
  • Added space support in the generated passwords.

04/October/2013 : Minor Update

  • Update structure slightly to improve performance. Now generates 50,000 80 character passwords in less than 10 seconds. (down from 30seconds)

16/October/2013 : Minor Update

Tags: , , ,