The following single function allows easy password generation in a number of styles:
Generate a random password.
count lets you generate multiple passwords at once. If count is not specified, a single string is returned. Otherwise, a list of count passwords will be returned.
alg lets you select the algorithm used when generating the password. This value may be either the name of a preset, or the name of an actual algorithm (see the list of presets and algorithms below).
Any additional keywords will be passed to the algorithm implementation.
The follow are some usages examples (your results may vary, depending on the random number state):
>>> from passlib.gen import generate_secret
>>> # generate a bunch of passwords using the gpw algorithm
>>> generate_secret(count=10, alg="gpw")
['preatioc',
'mirenencet',
'blackessse',
'shantesita',
'sonsimena',
'mestongesho',
'amilitterl',
'lonisantr',
'onsesenone',
'astensult']
>>> # generate a single alphanumeric password
>>> generate_secret(alg="alphanum")
'l9u09f3N8Squ23q2'
>>> # generate a single password using default algorithm
>>> generate_secret()
'bablistre'
>>> #generate a strong password
>>> generate_secret("strong")
"g_)'sP?Z'Zhi]6hL"
This function defines a number of presets, which can be passed in as the alg string, and will load the most appropriate underlying algorithm, as well as various presets:
- alphanum (the default)
- Generates a random sequence of mixed-case letters, and numbers. This was chosen as the default because it’s reasonably strong for average purposes, to lessen the security risk if users just call generate_secret() without options. If you want a more memorable (and therefore weaker) password, you have to explicitly chose another preset. As a matter of balance, this is not the strongest algorithm available, just one that’s reasonably strong for most uses.
- hex
- Generates a random sequence of 16 hexidecimal digits.
- ascii
- This algorithm uses a wide range of 92 ascii characters (letters, numbers, punctuation).
- human
This generates a medium-strength phonetic password, which should be (relatively) easy to remember, yet reasonably unlikely to be guessed.
Currently this uses the cvc algorithm with a size of 9-12, but if stronger-yet-memorable algorithm is found, this preset will be changed to refer to that algorithm instead.
- strong
This generates a password that’s as strong enough to be unguessable.
This is currently an alias for the ascii preset, which creates a 16 character password made up of all possible ascii characters. The odds of this password being guessed / generated again are 1 in 2e31, making it reasonably strong for most purposes. As computers get more powerful, this preset’s nature may be upped as needed to keep it strong.
The following algorithms are available:
- random
- This generates a random password from a specified alphabet. You can specify a charset of hex, alphanum, or ascii, or specifiy an alphabet string directly.
- cvc
- This implements a simple phonetic algorithm which generates sequences of letters using some basic english syllable patterns. While not as frequently pronouncable as the gpw algorithm’s results, this algorithm has a much larger effective key space, and so it a much better choice for phonetic password generation.
- gpw
This is a complex phonetic algorithm, which attempts to generate pronouncable words via a markov walk using a compiled dictionary of 3-order letter frequencies. This is a python implementation of Tom Van Vleck’s phonetic password algorithm, found at http://www.multicians.org/thvv/gpw.html.
Warning
While more memorable, it’s probablistic nature severely constrains the effective keyspace, so this should not be used where strong passwords are needed, especially if the attacker knows you have used this algorithm and can brute force their attack.
For size=10, this will generate a duplicate 1 out of 20,000 times.
This algorithm accepts a language keyword, letting you specify the dictionary to load from. The following languages are implemented:
- en_US (the default)
- uses a dictionary built from Ubuntu’s american-english word list.
- en_UK
- uses a dictionary built from Ubuntu’s british-english word list.
- gpw
- uses the original dictionary from Tom Van Vleck’s implementation.
Note
Due to keyspace issues, the en_US and en_UK tables have been removed for now.
The following keywords are recognized by all algorithms:
- size
- This lets you explicitly set the size of the generated password. If the size is not specified, a algorithm-dependant default is used. If size is a tuple of two integers, a random size is used with the specified range.
- reshake
- If true (the default), the random number generator will be
The following keywords are recognized by the phonetic algorithms (cvc,gpw):
- numeric_head
- If set to a positive integer, that many digits will be added to the start of the generated password.
- numeric_tail
- If set to a positive integer, that many digits will be added to the end of the generated password.
Todo
document internal classes