Debian Codegraph @

flufl/password/_generate.py

raw
1 # Copyright (C) 2011-2014 by Barry A. Warsaw
2 #
3 # This file is part of flufl.password
4 #
5 # flufl.password is free software: you can redistribute it and/or modify it
6 # under the terms of the GNU Lesser General Public License as published by the
7 # Free Software Foundation, version 3 of the License.
8 #
9 # flufl.password is distributed in the hope that it will be useful, but
10 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
12 # License for more details.
13 #
14 # You should have received a copy of the GNU Lesser General Public License
15 # along with flufl.password. If not, see <http://www.gnu.org/licenses/>.
16
17 """User-friendly password generation."""
18
19 from __future__ import absolute_import, print_function, unicode_literals
20
21 __metaclass__ = type
22 __all__ = [
23 'generate',
24 ]
25
26
27 import random
28
29 from itertools import chain, product
30 from string import ascii_lowercase
31
32
33 EMPTYSTRING = ''
34
35
36
37 _vowels = tuple('aeiou')
38 _consonants = tuple(c for c in ascii_lowercase if c not in _vowels)
39 _syllables = tuple(x + y for (x, y) in
40 chain(product(_vowels, _consonants),
41 product(_consonants, _vowels)))
42
43
44
45 def generate(length=8):
46 """Make a random *user friendly* password.
47
48 Such passwords are nominally easier to pronounce and thus remember. Their
49 security in relationship to purely random passwords has not been
50 determined.
51
52 :param length: Minimum length in characters for the resulting password.
53 The password will always be an even number of characters. The default
54 is to create passwords of length 8.
55 :type length: int
56 :return: The user friendly password.
57 :rtype: string
58 """
59 syllables = []
60 while len(syllables) * 2 < length:
61 syllables.append(random.choice(_syllables))
62 return EMPTYSTRING.join(syllables)[:length]