| 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 |
"""Make secrets.""" |
| 18 |
|
| 19 |
from __future__ import absolute_import, print_function, unicode_literals |
| 20 |
|
| 21 |
__metaclass__ = type |
| 22 |
__all__ = [ |
| 23 |
'make_secret', |
| 24 |
] |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
def make_secret(password, scheme=None): |
| 29 |
"""Return a hashed password using the given scheme. |
| 30 |
|
| 31 |
:param password: The plain text password to hash. |
| 32 |
:type password: bytes or utf-8 encoded string. |
| 33 |
:param scheme: The scheme class to use for encryption. If not given, |
| 34 |
`SSHAPasswordScheme` is used. |
| 35 |
:type scheme: PasswordScheme subclass. |
| 36 |
:return: The hashed password. |
| 37 |
:rtype: bytes |
| 38 |
""" |
| 39 |
if not isinstance(password, bytes): |
| 40 |
# If it's not a bytes, it better be a unicode in Python 2 or a str in |
| 41 |
# Python 3. Let encoding errors propagate up. |
| 42 |
password = password.encode('utf-8') |
| 43 |
if scheme is None: |
| 44 |
from flufl.password.schemes import SSHAPasswordScheme |
| 45 |
scheme = SSHAPasswordScheme |
| 46 |
secret = scheme.make_secret(password) |
| 47 |
if not isinstance(scheme.TAG, bytes): |
| 48 |
tag = scheme.TAG.encode('utf-8') |
| 49 |
return b'{' + tag + b'}' + secret |