| 1 |
================================ |
| 2 |
Using the flufl.password package |
| 3 |
================================ |
| 4 |
|
| 5 |
This package comes with a number of password hashing schemes. Some are more |
| 6 |
secure, while others provide for useful debugging. A hashed password follows |
| 7 |
the syntax promoted in `RFC 2307`_ (as best I can tell), having a basic format |
| 8 |
of ``{scheme}hashed_password``. |
| 9 |
|
| 10 |
|
| 11 |
Hashing a password |
| 12 |
================== |
| 13 |
|
| 14 |
You can create a secure hashed password using the default scheme, which |
| 15 |
includes random data. |
| 16 |
|
| 17 |
>>> from flufl.password import make_secret |
| 18 |
>>> show(make_secret('my password')) |
| 19 |
{SSHA}... |
| 20 |
|
| 21 |
You can also create a hashed password using one of the other built-in |
| 22 |
schemes. |
| 23 |
|
| 24 |
>>> from flufl.password.schemes import SHAPasswordScheme |
| 25 |
>>> show(make_secret('my password', SHAPasswordScheme)) |
| 26 |
{SHA}ovj3-hlaCAoipokEHaqPIET58zY= |
| 27 |
|
| 28 |
|
| 29 |
Available schemes |
| 30 |
----------------- |
| 31 |
|
| 32 |
There are several built-in schemes to choose from, which run the gamut from |
| 33 |
useful for debugging to variously higher levels of security. |
| 34 |
|
| 35 |
* The *no password* scheme throws away the password and always returns the |
| 36 |
empty string, but with a properly formatted password. |
| 37 |
|
| 38 |
>>> from flufl.password.schemes import NoPasswordScheme |
| 39 |
>>> show(make_secret('my password', NoPasswordScheme)) |
| 40 |
{NONE} |
| 41 |
|
| 42 |
* The *clear text* scheme returns the original password in clear text, but |
| 43 |
properly formatted. |
| 44 |
|
| 45 |
>>> from flufl.password.schemes import ClearTextPasswordScheme |
| 46 |
>>> show(make_secret('my password', ClearTextPasswordScheme)) |
| 47 |
{CLEARTEXT}my password |
| 48 |
|
| 49 |
* The *SHA1* password scheme encodes the SHA1 hash of the password. |
| 50 |
|
| 51 |
>>> show(make_secret('my password', SHAPasswordScheme)) |
| 52 |
{SHA}ovj3-hlaCAoipokEHaqPIET58zY= |
| 53 |
|
| 54 |
* The *salted SHA1* scheme adds a random salt to the password's digest. |
| 55 |
|
| 56 |
>>> from flufl.password.schemes import SSHAPasswordScheme |
| 57 |
>>> show(make_secret('my password', SSHAPasswordScheme)) |
| 58 |
{SSHA}... |
| 59 |
|
| 60 |
* There is an `RFC 2898`_ password encoding scheme. |
| 61 |
|
| 62 |
>>> from flufl.password.schemes import PBKDF2PasswordScheme |
| 63 |
>>> show(make_secret('my password', PBKDF2PasswordScheme)) |
| 64 |
{PBKDF2 SHA 2000}... |
| 65 |
|
| 66 |
|
| 67 |
Custom schemes |
| 68 |
-------------- |
| 69 |
|
| 70 |
It's also easy enough to create your own scheme. It must implement a static |
| 71 |
`make_secret()` method, which you can inherit from a common base class. The |
| 72 |
class must also have a `TAG` attribute which gives the unique name of this |
| 73 |
hashing scheme. |
| 74 |
|
| 75 |
The scheme should be registered so that it can be found by its tag for |
| 76 |
verification purposes. This can be done using the `@register` descriptor. |
| 77 |
:: |
| 78 |
|
| 79 |
>>> from codecs import getencoder |
| 80 |
>>> from flufl.password import register |
| 81 |
>>> from flufl.password.schemes import PasswordScheme |
| 82 |
|
| 83 |
>>> @register |
| 84 |
... class MyScheme(PasswordScheme): |
| 85 |
... TAG = 'CAESAR' |
| 86 |
... @staticmethod |
| 87 |
... def make_secret(password): |
| 88 |
... # In Python 3, this is a string-to-string encoding. The |
| 89 |
... # caller already turned `password` into a byte string, so |
| 90 |
... # we have to pass it back through a string to rotate it. |
| 91 |
... # We also can't just call .encode('rot_13') on the string |
| 92 |
... # because Python 3.2 chokes on the returned string (it expects |
| 93 |
... # a bytes object to be returned). Sigh. |
| 94 |
... as_string = password.decode('utf-8') |
| 95 |
... encoder = getencoder('rot_13') |
| 96 |
... return encoder(as_string)[0].encode('utf-8') |
| 97 |
|
| 98 |
>>> show(make_secret('my password', MyScheme)) |
| 99 |
{CAESAR}zl cnffjbeq |
| 100 |
|
| 101 |
Hashed passwords are always bytes. |
| 102 |
|
| 103 |
>>> isinstance(make_secret('my password', MyScheme), bytes) |
| 104 |
True |
| 105 |
|
| 106 |
|
| 107 |
Verifying a password |
| 108 |
==================== |
| 109 |
|
| 110 |
When the user entered their original password, you hashed it using one of the |
| 111 |
schemes mentioned above. You are only storing this hashed password in your |
| 112 |
database. |
| 113 |
|
| 114 |
The user now wants to log in, so she provides you with her plain text |
| 115 |
password. You want to see if they match. |
| 116 |
|
| 117 |
The easiest way to do this is to give both the plain text password the user |
| 118 |
just typed, and the hash password you have in your database. |
| 119 |
|
| 120 |
>>> from flufl.password import verify |
| 121 |
>>> verify(b'{SHA}ovj3-hlaCAoipokEHaqPIET58zY=', 'my password') |
| 122 |
True |
| 123 |
|
| 124 |
Of course, if they enter the wrong password, it does not verify. |
| 125 |
|
| 126 |
>>> verify(b'{SHA}ovj3-hlaCAoipokEHaqPIET58zY=', 'your password') |
| 127 |
False |
| 128 |
|
| 129 |
Your custom hashing scheme must implement the `check_response()` API in order |
| 130 |
to support password verification. The `PasswordScheme` base class supports |
| 131 |
the most obvious implementation of this, which serves for most schemes. For |
| 132 |
example, the Caesar scheme does not need to implement a `check_response()` |
| 133 |
method. |
| 134 |
|
| 135 |
>>> verify(b'{CAESAR}zl cnffjbeq', 'my password') |
| 136 |
True |
| 137 |
|
| 138 |
|
| 139 |
User-friendly passwords |
| 140 |
======================= |
| 141 |
|
| 142 |
This package also provides a convenient utility for generating *user friendly* |
| 143 |
passwords. These passwords gather random input and translate them into pairs |
| 144 |
of vowel-consonant (or consonant-vowel) syllables. It then strings together |
| 145 |
enough of these syllables to match the requested password length. In theory, |
| 146 |
this produces relatively secure passwords that are easier to pronounce and |
| 147 |
remember. The security claims of these generated passwords have not been |
| 148 |
evaluated. |
| 149 |
|
| 150 |
>>> from flufl.password import generate |
| 151 |
>>> my_password = generate(10) |
| 152 |
>>> len(my_password) |
| 153 |
10 |
| 154 |
>>> sum(1 for c in my_password if c in 'aeiou') |
| 155 |
5 |
| 156 |
>>> sum(1 for c in my_password if c not in 'aeiou') |
| 157 |
5 |
| 158 |
|
| 159 |
|
| 160 |
.. _`RFC 2307`: http://www.faqs.org/rfcs/rfc2307.html |
| 161 |
.. _`RFC 2898`: http://www.faqs.org/rfcs/rfc2898.html |