File: passwordgen.py

package info (click to toggle)
python-jtoolkit 0.7.8-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 1,436 kB
  • ctags: 2,536
  • sloc: python: 15,143; makefile: 20
file content (84 lines) | stat: -rwxr-xr-x 2,745 bytes parent folder | download | duplicates (2)
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
# -*- coding: utf-8 -*-
##
## passwd.py v0.1
##
## modified by David Fraser <davidf@sjsoft.com>, 2001-08-10
## based on pgenerate.py (Author and description below)
## simple func generates an english-looking lowercase word with no funny chars
##
## Randomly creates a password with specified length, or picks
## a password from a dictionary. Also randomly warps the characters,
## making passwords from a dictionary more or less readable but
## slightly more difficult to crack.
##
##
## Author: Rikard Bosnjakovic <bos@hack.org>, 2001-06-12
##
from whrandom import choice, randint

# choose your dictionary
dictionary_file = '/usr/share/dict/words'

def createpassword(min_chars = 6, max_chars = 8):
    """createpassword([min_chars = 6, max_chars = 8):
Picks a password from a dictionary and warps it, with minimum and maximum chars as specified (default 6,8)."""

    # Get a word from a dictionary with minimum [min_chars] and maximum [max_chars] characters.
    word  = ""
    words = open(dictionary_file, "r").readlines()
    while (len(word) < min_chars) or (len(word) > max_chars):
        word = choice(words)

    password = word.lower().strip()

    # Warps around the chars in the password.
    import string

    warps = {}
    # add the alphabet to the warplist
    for x in xrange(ord('a'), ord('z')+1):
        x = chr(x)
        warps[x] = [x]

    # add some specials
    specialchars = (("a", ["e", "y"]),
                    ("b", ["p", "m"]),
                    ("c", ["k", "s"]),
                    ("d", ["t", "n"]),
                    ("e", ["i"]),
                    ("f", ["v", "ph"]),
                    ("g", ["k", "ng"]),
                    ("i", ["y"]),
                    ("j", ["z"]),
                    ("k", ["g"]),
                    ("l", ["y"]),
                    ("m", ["b", "p"]),
                    ("n", ["d", "t"]),
                    ("o", ["u"]),
                    ("p", ["b", "m"]),
                    ("q", ["k"]),
                    ("s", ["z"]),
                    ("t", ["d", "n"]),
                    ("u", ["o", "a"]),
                    ("v", ["f"]),
                    ("x", ["k"]),
                    ("z", ["s"]))

    for (a,b) in specialchars:
      warps[a] += b

    warped_password = ""
    # warp the chars in the password
    for i in password:
        if i in warps.keys():
            warped = password.startswith(warped_password)
            # 50% probability if warped, 75% if as yet unwarped
            if (warped and randint(0, 3)) or ((not warped) and  randint(0, 1)):
                warped_password += choice(warps[i])
            else:
                warped_password += i
        else:
            warped_password += i

    return warped_password