| 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 |
"""Verification.""" |
| 18 |
|
| 19 |
from __future__ import absolute_import, print_function, unicode_literals |
| 20 |
|
| 21 |
__metaclass__ = type |
| 22 |
__all__ = [ |
| 23 |
'verify', |
| 24 |
] |
| 25 |
|
| 26 |
|
| 27 |
from flufl.password._registry import lookup |
| 28 |
from flufl.password._utils import split |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
def verify(hashed, cleartext): |
| 33 |
"""Verify whether the clear text response matches the hashed challenge. |
| 34 |
|
| 35 |
:param hashed: The hashed password. |
| 36 |
:type hashed: bytes |
| 37 |
:param cleartext: The clear text password to match against. |
| 38 |
:type cleartext: bytes or utf-8 encoded string. |
| 39 |
:return: Flag indicating whether the password matched. |
| 40 |
:rtype: bool |
| 41 |
:raises BadPasswordSchemeError: when the indicated scheme cannot be found. |
| 42 |
:raises BadPasswordFormatError: when the hashed parameter is not properly |
| 43 |
formatted as ``{SCHEME}password``. |
| 44 |
""" |
| 45 |
if not isinstance(cleartext, bytes): |
| 46 |
cleartext = cleartext.encode('utf-8') |
| 47 |
scheme, secret = split(hashed) |
| 48 |
scheme_class = lookup(scheme.upper()) |
| 49 |
return scheme_class.check_response(secret, cleartext, scheme) |