| 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 |
"""Various utilities.""" |
| 18 |
|
| 19 |
from __future__ import absolute_import, print_function, unicode_literals |
| 20 |
|
| 21 |
__metaclass__ = type |
| 22 |
__all__ = [ |
| 23 |
'BadPasswordFormatError', |
| 24 |
'split', |
| 25 |
] |
| 26 |
|
| 27 |
|
| 28 |
import re |
| 29 |
|
| 30 |
SCHEME_RE = r'{(?P<scheme>[^}]+?)}(?P<rest>.*)'.encode() |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
class BadPasswordFormatError(Exception): |
| 35 |
"""A badly formatted password hash was given.""" |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
def split(hashed): |
| 40 |
mo = re.match(SCHEME_RE, hashed, re.IGNORECASE) |
| 41 |
if not mo: |
| 42 |
raise BadPasswordFormatError |
| 43 |
scheme, secret = mo.groups(('scheme', 'rest')) |
| 44 |
return scheme.decode('utf-8'), secret |