2012-06-08 00:08:47 +08:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2012-08-09 22:25:35 +08:00
|
|
|
import base64
|
2013-03-26 23:44:26 +08:00
|
|
|
import binascii
|
2011-12-23 11:53:56 +08:00
|
|
|
import hashlib
|
2013-07-29 21:50:58 +08:00
|
|
|
import importlib
|
2015-01-28 20:35:27 +08:00
|
|
|
from collections import OrderedDict
|
2011-12-23 11:53:56 +08:00
|
|
|
|
|
|
|
from django.conf import settings
|
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.core.signals import setting_changed
|
|
|
|
from django.dispatch import receiver
|
2014-11-19 04:45:12 +08:00
|
|
|
from django.utils import lru_cache
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.utils.crypto import (
|
|
|
|
constant_time_compare, get_random_string, pbkdf2,
|
|
|
|
)
|
|
|
|
from django.utils.encoding import force_bytes, force_str, force_text
|
2014-01-21 04:15:14 +08:00
|
|
|
from django.utils.module_loading import import_string
|
2011-12-25 03:38:37 +08:00
|
|
|
from django.utils.translation import ugettext_noop as _
|
2011-12-23 11:53:56 +08:00
|
|
|
|
2013-06-19 02:02:00 +08:00
|
|
|
UNUSABLE_PASSWORD_PREFIX = '!' # This will never be a valid encoded hash
|
|
|
|
UNUSABLE_PASSWORD_SUFFIX_LENGTH = 40 # number of random chars to add after UNUSABLE_PASSWORD_PREFIX
|
2012-05-01 02:35:04 +08:00
|
|
|
|
2011-12-23 11:53:56 +08:00
|
|
|
|
|
|
|
def is_password_usable(encoded):
|
2013-06-19 02:02:00 +08:00
|
|
|
if encoded is None or encoded.startswith(UNUSABLE_PASSWORD_PREFIX):
|
2012-09-12 17:21:58 +08:00
|
|
|
return False
|
|
|
|
try:
|
2013-07-04 01:13:47 +08:00
|
|
|
identify_hasher(encoded)
|
2012-09-12 17:21:58 +08:00
|
|
|
except ValueError:
|
|
|
|
return False
|
|
|
|
return True
|
2011-12-23 11:53:56 +08:00
|
|
|
|
|
|
|
|
|
|
|
def check_password(password, encoded, setter=None, preferred='default'):
|
|
|
|
"""
|
|
|
|
Returns a boolean of whether the raw password matches the three
|
|
|
|
part encoded digest.
|
|
|
|
|
|
|
|
If setter is specified, it'll be called when you need to
|
|
|
|
regenerate the password.
|
|
|
|
"""
|
2013-07-04 01:13:47 +08:00
|
|
|
if password is None or not is_password_usable(encoded):
|
2011-12-23 11:53:56 +08:00
|
|
|
return False
|
|
|
|
|
|
|
|
preferred = get_hasher(preferred)
|
2012-06-06 17:06:33 +08:00
|
|
|
hasher = identify_hasher(encoded)
|
2011-12-23 11:53:56 +08:00
|
|
|
|
|
|
|
must_update = hasher.algorithm != preferred.algorithm
|
2013-09-25 02:52:20 +08:00
|
|
|
if not must_update:
|
2013-11-30 09:49:56 +08:00
|
|
|
must_update = preferred.must_update(encoded)
|
2011-12-23 11:53:56 +08:00
|
|
|
is_correct = hasher.verify(password, encoded)
|
|
|
|
if setter and is_correct and must_update:
|
2012-06-06 16:53:16 +08:00
|
|
|
setter(password)
|
2011-12-23 11:53:56 +08:00
|
|
|
return is_correct
|
|
|
|
|
|
|
|
|
|
|
|
def make_password(password, salt=None, hasher='default'):
|
|
|
|
"""
|
|
|
|
Turn a plain-text password into a hash for database storage
|
|
|
|
|
2013-06-19 02:02:00 +08:00
|
|
|
Same as encode() but generates a new random salt.
|
|
|
|
If password is None then a concatenation of
|
|
|
|
UNUSABLE_PASSWORD_PREFIX and a random string will be returned
|
|
|
|
which disallows logins. Additional random string reduces chances
|
|
|
|
of gaining access to staff or superuser accounts.
|
|
|
|
See ticket #20079 for more info.
|
2011-12-23 11:53:56 +08:00
|
|
|
"""
|
2013-06-18 00:06:26 +08:00
|
|
|
if password is None:
|
2013-06-19 02:02:00 +08:00
|
|
|
return UNUSABLE_PASSWORD_PREFIX + get_random_string(UNUSABLE_PASSWORD_SUFFIX_LENGTH)
|
2011-12-23 11:53:56 +08:00
|
|
|
hasher = get_hasher(hasher)
|
|
|
|
|
|
|
|
if not salt:
|
|
|
|
salt = hasher.salt()
|
|
|
|
|
|
|
|
return hasher.encode(password, salt)
|
|
|
|
|
|
|
|
|
2014-11-19 04:45:12 +08:00
|
|
|
@lru_cache.lru_cache()
|
|
|
|
def get_hashers():
|
2011-12-23 11:53:56 +08:00
|
|
|
hashers = []
|
2014-11-19 04:45:12 +08:00
|
|
|
for hasher_path in settings.PASSWORD_HASHERS:
|
|
|
|
hasher_cls = import_string(hasher_path)
|
|
|
|
hasher = hasher_cls()
|
2011-12-23 11:53:56 +08:00
|
|
|
if not getattr(hasher, 'algorithm'):
|
|
|
|
raise ImproperlyConfigured("hasher doesn't specify an "
|
2014-11-19 04:45:12 +08:00
|
|
|
"algorithm name: %s" % hasher_path)
|
2011-12-23 11:53:56 +08:00
|
|
|
hashers.append(hasher)
|
2014-11-19 04:45:12 +08:00
|
|
|
return hashers
|
|
|
|
|
|
|
|
|
|
|
|
@lru_cache.lru_cache()
|
|
|
|
def get_hashers_by_algorithm():
|
|
|
|
return {hasher.algorithm: hasher for hasher in get_hashers()}
|
|
|
|
|
|
|
|
|
|
|
|
@receiver(setting_changed)
|
|
|
|
def reset_hashers(**kwargs):
|
|
|
|
if kwargs['setting'] == 'PASSWORD_HASHERS':
|
|
|
|
get_hashers.cache_clear()
|
|
|
|
get_hashers_by_algorithm.cache_clear()
|
2011-12-23 11:53:56 +08:00
|
|
|
|
|
|
|
|
|
|
|
def get_hasher(algorithm='default'):
|
|
|
|
"""
|
|
|
|
Returns an instance of a loaded password hasher.
|
|
|
|
|
|
|
|
If algorithm is 'default', the default hasher will be returned.
|
|
|
|
This function will also lazy import hashers specified in your
|
|
|
|
settings file if needed.
|
|
|
|
"""
|
|
|
|
if hasattr(algorithm, 'algorithm'):
|
|
|
|
return algorithm
|
|
|
|
|
|
|
|
elif algorithm == 'default':
|
2014-11-19 04:45:12 +08:00
|
|
|
return get_hashers()[0]
|
|
|
|
|
2011-12-23 11:53:56 +08:00
|
|
|
else:
|
2014-11-19 04:45:12 +08:00
|
|
|
hashers = get_hashers_by_algorithm()
|
|
|
|
try:
|
|
|
|
return hashers[algorithm]
|
|
|
|
except KeyError:
|
2011-12-23 11:53:56 +08:00
|
|
|
raise ValueError("Unknown password hashing algorithm '%s'. "
|
|
|
|
"Did you specify it in the PASSWORD_HASHERS "
|
|
|
|
"setting?" % algorithm)
|
|
|
|
|
|
|
|
|
2012-06-06 17:06:33 +08:00
|
|
|
def identify_hasher(encoded):
|
|
|
|
"""
|
|
|
|
Returns an instance of a loaded password hasher.
|
|
|
|
|
|
|
|
Identifies hasher algorithm by examining encoded hash, and calls
|
|
|
|
get_hasher() to return hasher. Raises ValueError if
|
|
|
|
algorithm cannot be identified, or if hasher is not loaded.
|
|
|
|
"""
|
2013-02-26 03:01:57 +08:00
|
|
|
# Ancient versions of Django created plain MD5 passwords and accepted
|
|
|
|
# MD5 passwords with an empty salt.
|
2013-02-02 18:57:25 +08:00
|
|
|
if ((len(encoded) == 32 and '$' not in encoded) or
|
2013-02-02 19:13:32 +08:00
|
|
|
(len(encoded) == 37 and encoded.startswith('md5$$'))):
|
2012-06-06 17:06:33 +08:00
|
|
|
algorithm = 'unsalted_md5'
|
2013-02-26 03:01:57 +08:00
|
|
|
# Ancient versions of Django accepted SHA1 passwords with an empty salt.
|
|
|
|
elif len(encoded) == 46 and encoded.startswith('sha1$$'):
|
|
|
|
algorithm = 'unsalted_sha1'
|
2012-06-06 17:06:33 +08:00
|
|
|
else:
|
|
|
|
algorithm = encoded.split('$', 1)[0]
|
|
|
|
return get_hasher(algorithm)
|
|
|
|
|
|
|
|
|
2011-12-23 11:53:56 +08:00
|
|
|
def mask_hash(hash, show=6, char="*"):
|
|
|
|
"""
|
|
|
|
Returns the given hash, with only the first ``show`` number shown. The
|
|
|
|
rest are masked with ``char`` for security reasons.
|
|
|
|
"""
|
|
|
|
masked = hash[:show]
|
|
|
|
masked += char * len(hash[show:])
|
|
|
|
return masked
|
|
|
|
|
|
|
|
|
|
|
|
class BasePasswordHasher(object):
|
|
|
|
"""
|
|
|
|
Abstract base class for password hashers
|
|
|
|
|
|
|
|
When creating your own hasher, you need to override algorithm,
|
|
|
|
verify(), encode() and safe_summary().
|
|
|
|
|
|
|
|
PasswordHasher objects are immutable.
|
|
|
|
"""
|
|
|
|
algorithm = None
|
|
|
|
library = None
|
|
|
|
|
|
|
|
def _load_library(self):
|
|
|
|
if self.library is not None:
|
|
|
|
if isinstance(self.library, (tuple, list)):
|
|
|
|
name, mod_path = self.library
|
|
|
|
else:
|
2013-08-05 00:17:10 +08:00
|
|
|
mod_path = self.library
|
2011-12-23 11:53:56 +08:00
|
|
|
try:
|
|
|
|
module = importlib.import_module(mod_path)
|
2013-06-14 22:19:53 +08:00
|
|
|
except ImportError as e:
|
|
|
|
raise ValueError("Couldn't load %r algorithm library: %s" %
|
|
|
|
(self.__class__.__name__, e))
|
2011-12-23 11:53:56 +08:00
|
|
|
return module
|
2013-06-14 22:19:53 +08:00
|
|
|
raise ValueError("Hasher %r doesn't specify a library attribute" %
|
|
|
|
self.__class__.__name__)
|
2011-12-23 11:53:56 +08:00
|
|
|
|
|
|
|
def salt(self):
|
|
|
|
"""
|
2014-03-02 22:25:53 +08:00
|
|
|
Generates a cryptographically secure nonce salt in ASCII
|
2011-12-23 11:53:56 +08:00
|
|
|
"""
|
|
|
|
return get_random_string()
|
|
|
|
|
|
|
|
def verify(self, password, encoded):
|
|
|
|
"""
|
|
|
|
Checks if the given password is correct
|
|
|
|
"""
|
2013-09-07 02:24:52 +08:00
|
|
|
raise NotImplementedError('subclasses of BasePasswordHasher must provide a verify() method')
|
2011-12-23 11:53:56 +08:00
|
|
|
|
|
|
|
def encode(self, password, salt):
|
|
|
|
"""
|
|
|
|
Creates an encoded database value
|
|
|
|
|
|
|
|
The result is normally formatted as "algorithm$salt$hash" and
|
|
|
|
must be fewer than 128 characters.
|
|
|
|
"""
|
2013-09-07 02:24:52 +08:00
|
|
|
raise NotImplementedError('subclasses of BasePasswordHasher must provide an encode() method')
|
2011-12-23 11:53:56 +08:00
|
|
|
|
|
|
|
def safe_summary(self, encoded):
|
|
|
|
"""
|
|
|
|
Returns a summary of safe values
|
|
|
|
|
|
|
|
The result is a dictionary and will be used where the password field
|
|
|
|
must be displayed to construct a safe representation of the password.
|
|
|
|
"""
|
2013-09-07 02:24:52 +08:00
|
|
|
raise NotImplementedError('subclasses of BasePasswordHasher must provide a safe_summary() method')
|
2011-12-23 11:53:56 +08:00
|
|
|
|
2013-09-25 02:52:20 +08:00
|
|
|
def must_update(self, encoded):
|
|
|
|
return False
|
|
|
|
|
2011-12-23 11:53:56 +08:00
|
|
|
|
|
|
|
class PBKDF2PasswordHasher(BasePasswordHasher):
|
|
|
|
"""
|
|
|
|
Secure password hashing using the PBKDF2 algorithm (recommended)
|
|
|
|
|
2015-02-21 05:37:03 +08:00
|
|
|
Configured to use PBKDF2 + HMAC + SHA256.
|
2011-12-23 11:53:56 +08:00
|
|
|
The result is a 64 byte binary string. Iterations may be changed
|
|
|
|
safely but you must rename the algorithm if you change SHA256.
|
|
|
|
"""
|
|
|
|
algorithm = "pbkdf2_sha256"
|
2015-01-17 08:27:10 +08:00
|
|
|
iterations = 24000
|
2011-12-23 11:53:56 +08:00
|
|
|
digest = hashlib.sha256
|
|
|
|
|
|
|
|
def encode(self, password, salt, iterations=None):
|
2013-06-18 00:06:26 +08:00
|
|
|
assert password is not None
|
2011-12-23 11:53:56 +08:00
|
|
|
assert salt and '$' not in salt
|
|
|
|
if not iterations:
|
|
|
|
iterations = self.iterations
|
|
|
|
hash = pbkdf2(password, salt, iterations, digest=self.digest)
|
2012-08-11 06:18:33 +08:00
|
|
|
hash = base64.b64encode(hash).decode('ascii').strip()
|
2011-12-23 11:53:56 +08:00
|
|
|
return "%s$%d$%s$%s" % (self.algorithm, iterations, salt, hash)
|
|
|
|
|
|
|
|
def verify(self, password, encoded):
|
|
|
|
algorithm, iterations, salt, hash = encoded.split('$', 3)
|
|
|
|
assert algorithm == self.algorithm
|
|
|
|
encoded_2 = self.encode(password, salt, int(iterations))
|
|
|
|
return constant_time_compare(encoded, encoded_2)
|
|
|
|
|
|
|
|
def safe_summary(self, encoded):
|
|
|
|
algorithm, iterations, salt, hash = encoded.split('$', 3)
|
|
|
|
assert algorithm == self.algorithm
|
2013-08-03 13:41:15 +08:00
|
|
|
return OrderedDict([
|
2011-12-25 03:38:37 +08:00
|
|
|
(_('algorithm'), algorithm),
|
|
|
|
(_('iterations'), iterations),
|
|
|
|
(_('salt'), mask_hash(salt)),
|
|
|
|
(_('hash'), mask_hash(hash)),
|
2011-12-23 11:53:56 +08:00
|
|
|
])
|
|
|
|
|
2013-09-25 02:52:20 +08:00
|
|
|
def must_update(self, encoded):
|
|
|
|
algorithm, iterations, salt, hash = encoded.split('$', 3)
|
|
|
|
return int(iterations) != self.iterations
|
|
|
|
|
2011-12-23 11:53:56 +08:00
|
|
|
|
|
|
|
class PBKDF2SHA1PasswordHasher(PBKDF2PasswordHasher):
|
|
|
|
"""
|
|
|
|
Alternate PBKDF2 hasher which uses SHA1, the default PRF
|
|
|
|
recommended by PKCS #5. This is compatible with other
|
|
|
|
implementations of PBKDF2, such as openssl's
|
|
|
|
PKCS5_PBKDF2_HMAC_SHA1().
|
|
|
|
"""
|
|
|
|
algorithm = "pbkdf2_sha1"
|
|
|
|
digest = hashlib.sha1
|
|
|
|
|
|
|
|
|
2013-03-26 23:44:26 +08:00
|
|
|
class BCryptSHA256PasswordHasher(BasePasswordHasher):
|
2011-12-23 11:53:56 +08:00
|
|
|
"""
|
|
|
|
Secure password hashing using the bcrypt algorithm (recommended)
|
|
|
|
|
|
|
|
This is considered by many to be the most secure algorithm but you
|
2013-05-14 11:39:50 +08:00
|
|
|
must first install the bcrypt library. Please be warned that
|
2011-12-23 11:53:56 +08:00
|
|
|
this library depends on native C code and might cause portability
|
|
|
|
issues.
|
|
|
|
"""
|
2013-03-26 23:44:26 +08:00
|
|
|
algorithm = "bcrypt_sha256"
|
|
|
|
digest = hashlib.sha256
|
2013-05-14 11:39:50 +08:00
|
|
|
library = ("bcrypt", "bcrypt")
|
2011-12-23 11:53:56 +08:00
|
|
|
rounds = 12
|
|
|
|
|
|
|
|
def salt(self):
|
|
|
|
bcrypt = self._load_library()
|
|
|
|
return bcrypt.gensalt(self.rounds)
|
|
|
|
|
|
|
|
def encode(self, password, salt):
|
|
|
|
bcrypt = self._load_library()
|
2012-12-22 23:00:15 +08:00
|
|
|
# Need to reevaluate the force_bytes call once bcrypt is supported on
|
|
|
|
# Python 3
|
2013-03-26 23:44:26 +08:00
|
|
|
|
|
|
|
# Hash the password prior to using bcrypt to prevent password truncation
|
|
|
|
# See: https://code.djangoproject.com/ticket/20138
|
|
|
|
if self.digest is not None:
|
|
|
|
# We use binascii.hexlify here because Python3 decided that a hex encoded
|
|
|
|
# bytestring is somehow a unicode.
|
|
|
|
password = binascii.hexlify(self.digest(force_bytes(password)).digest())
|
|
|
|
else:
|
|
|
|
password = force_bytes(password)
|
|
|
|
|
|
|
|
data = bcrypt.hashpw(password, salt)
|
2013-05-11 13:33:10 +08:00
|
|
|
return "%s$%s" % (self.algorithm, force_text(data))
|
2011-12-23 11:53:56 +08:00
|
|
|
|
|
|
|
def verify(self, password, encoded):
|
|
|
|
algorithm, data = encoded.split('$', 1)
|
|
|
|
assert algorithm == self.algorithm
|
|
|
|
bcrypt = self._load_library()
|
2013-03-26 23:44:26 +08:00
|
|
|
|
|
|
|
# Hash the password prior to using bcrypt to prevent password truncation
|
|
|
|
# See: https://code.djangoproject.com/ticket/20138
|
|
|
|
if self.digest is not None:
|
|
|
|
# We use binascii.hexlify here because Python3 decided that a hex encoded
|
|
|
|
# bytestring is somehow a unicode.
|
|
|
|
password = binascii.hexlify(self.digest(force_bytes(password)).digest())
|
|
|
|
else:
|
|
|
|
password = force_bytes(password)
|
|
|
|
|
2013-05-11 13:33:10 +08:00
|
|
|
# Ensure that our data is a bytestring
|
|
|
|
data = force_bytes(data)
|
2013-11-09 19:36:10 +08:00
|
|
|
# force_bytes() necessary for py-bcrypt compatibility
|
|
|
|
hashpw = force_bytes(bcrypt.hashpw(password, data))
|
2013-05-11 13:33:10 +08:00
|
|
|
|
2013-11-09 19:36:10 +08:00
|
|
|
return constant_time_compare(data, hashpw)
|
2011-12-23 11:53:56 +08:00
|
|
|
|
|
|
|
def safe_summary(self, encoded):
|
|
|
|
algorithm, empty, algostr, work_factor, data = encoded.split('$', 4)
|
|
|
|
assert algorithm == self.algorithm
|
|
|
|
salt, checksum = data[:22], data[22:]
|
2013-08-03 13:41:15 +08:00
|
|
|
return OrderedDict([
|
2011-12-25 03:38:37 +08:00
|
|
|
(_('algorithm'), algorithm),
|
|
|
|
(_('work factor'), work_factor),
|
|
|
|
(_('salt'), mask_hash(salt)),
|
|
|
|
(_('checksum'), mask_hash(checksum)),
|
2011-12-23 11:53:56 +08:00
|
|
|
])
|
|
|
|
|
2015-02-27 03:04:24 +08:00
|
|
|
def must_update(self, encoded):
|
|
|
|
algorithm, empty, algostr, rounds, data = encoded.split('$', 4)
|
|
|
|
return int(rounds) != self.rounds
|
|
|
|
|
2011-12-23 11:53:56 +08:00
|
|
|
|
2013-03-26 23:44:26 +08:00
|
|
|
class BCryptPasswordHasher(BCryptSHA256PasswordHasher):
|
|
|
|
"""
|
|
|
|
Secure password hashing using the bcrypt algorithm
|
|
|
|
|
|
|
|
This is considered by many to be the most secure algorithm but you
|
2013-05-14 11:39:50 +08:00
|
|
|
must first install the bcrypt library. Please be warned that
|
2013-03-26 23:44:26 +08:00
|
|
|
this library depends on native C code and might cause portability
|
|
|
|
issues.
|
|
|
|
|
|
|
|
This hasher does not first hash the password which means it is subject to
|
|
|
|
the 72 character bcrypt password truncation, most use cases should prefer
|
|
|
|
the BCryptSha512PasswordHasher.
|
|
|
|
|
|
|
|
See: https://code.djangoproject.com/ticket/20138
|
|
|
|
"""
|
|
|
|
algorithm = "bcrypt"
|
|
|
|
digest = None
|
|
|
|
|
|
|
|
|
2011-12-23 11:53:56 +08:00
|
|
|
class SHA1PasswordHasher(BasePasswordHasher):
|
|
|
|
"""
|
|
|
|
The SHA1 password hashing algorithm (not recommended)
|
|
|
|
"""
|
|
|
|
algorithm = "sha1"
|
|
|
|
|
|
|
|
def encode(self, password, salt):
|
2013-06-18 00:06:26 +08:00
|
|
|
assert password is not None
|
2011-12-23 11:53:56 +08:00
|
|
|
assert salt and '$' not in salt
|
2012-08-29 02:59:56 +08:00
|
|
|
hash = hashlib.sha1(force_bytes(salt + password)).hexdigest()
|
2011-12-23 11:53:56 +08:00
|
|
|
return "%s$%s$%s" % (self.algorithm, salt, hash)
|
|
|
|
|
|
|
|
def verify(self, password, encoded):
|
|
|
|
algorithm, salt, hash = encoded.split('$', 2)
|
|
|
|
assert algorithm == self.algorithm
|
|
|
|
encoded_2 = self.encode(password, salt)
|
|
|
|
return constant_time_compare(encoded, encoded_2)
|
|
|
|
|
|
|
|
def safe_summary(self, encoded):
|
|
|
|
algorithm, salt, hash = encoded.split('$', 2)
|
|
|
|
assert algorithm == self.algorithm
|
2013-08-03 13:41:15 +08:00
|
|
|
return OrderedDict([
|
2011-12-25 03:38:37 +08:00
|
|
|
(_('algorithm'), algorithm),
|
|
|
|
(_('salt'), mask_hash(salt, show=2)),
|
|
|
|
(_('hash'), mask_hash(hash)),
|
2011-12-23 11:53:56 +08:00
|
|
|
])
|
|
|
|
|
|
|
|
|
|
|
|
class MD5PasswordHasher(BasePasswordHasher):
|
2012-03-01 04:12:16 +08:00
|
|
|
"""
|
|
|
|
The Salted MD5 password hashing algorithm (not recommended)
|
|
|
|
"""
|
|
|
|
algorithm = "md5"
|
|
|
|
|
|
|
|
def encode(self, password, salt):
|
2013-06-18 00:06:26 +08:00
|
|
|
assert password is not None
|
2012-03-01 04:12:16 +08:00
|
|
|
assert salt and '$' not in salt
|
2012-08-29 02:59:56 +08:00
|
|
|
hash = hashlib.md5(force_bytes(salt + password)).hexdigest()
|
2012-03-01 04:12:16 +08:00
|
|
|
return "%s$%s$%s" % (self.algorithm, salt, hash)
|
|
|
|
|
|
|
|
def verify(self, password, encoded):
|
|
|
|
algorithm, salt, hash = encoded.split('$', 2)
|
|
|
|
assert algorithm == self.algorithm
|
|
|
|
encoded_2 = self.encode(password, salt)
|
|
|
|
return constant_time_compare(encoded, encoded_2)
|
|
|
|
|
|
|
|
def safe_summary(self, encoded):
|
|
|
|
algorithm, salt, hash = encoded.split('$', 2)
|
|
|
|
assert algorithm == self.algorithm
|
2013-08-03 13:41:15 +08:00
|
|
|
return OrderedDict([
|
2012-03-01 04:12:16 +08:00
|
|
|
(_('algorithm'), algorithm),
|
|
|
|
(_('salt'), mask_hash(salt, show=2)),
|
|
|
|
(_('hash'), mask_hash(hash)),
|
|
|
|
])
|
|
|
|
|
|
|
|
|
2013-02-26 03:01:57 +08:00
|
|
|
class UnsaltedSHA1PasswordHasher(BasePasswordHasher):
|
|
|
|
"""
|
|
|
|
Very insecure algorithm that you should *never* use; stores SHA1 hashes
|
|
|
|
with an empty salt.
|
|
|
|
|
|
|
|
This class is implemented because Django used to accept such password
|
|
|
|
hashes. Some older Django installs still have these values lingering
|
|
|
|
around so we need to handle and upgrade them properly.
|
2011-12-23 11:53:56 +08:00
|
|
|
"""
|
2013-02-26 03:01:57 +08:00
|
|
|
algorithm = "unsalted_sha1"
|
|
|
|
|
|
|
|
def salt(self):
|
|
|
|
return ''
|
2011-12-23 11:53:56 +08:00
|
|
|
|
2013-02-26 03:01:57 +08:00
|
|
|
def encode(self, password, salt):
|
|
|
|
assert salt == ''
|
|
|
|
hash = hashlib.sha1(force_bytes(password)).hexdigest()
|
|
|
|
return 'sha1$$%s' % hash
|
|
|
|
|
|
|
|
def verify(self, password, encoded):
|
|
|
|
encoded_2 = self.encode(password, '')
|
|
|
|
return constant_time_compare(encoded, encoded_2)
|
|
|
|
|
|
|
|
def safe_summary(self, encoded):
|
|
|
|
assert encoded.startswith('sha1$$')
|
|
|
|
hash = encoded[6:]
|
2013-08-03 13:41:15 +08:00
|
|
|
return OrderedDict([
|
2013-02-26 03:01:57 +08:00
|
|
|
(_('algorithm'), self.algorithm),
|
|
|
|
(_('hash'), mask_hash(hash)),
|
|
|
|
])
|
|
|
|
|
|
|
|
|
|
|
|
class UnsaltedMD5PasswordHasher(BasePasswordHasher):
|
|
|
|
"""
|
|
|
|
Incredibly insecure algorithm that you should *never* use; stores unsalted
|
|
|
|
MD5 hashes without the algorithm prefix, also accepts MD5 hashes with an
|
|
|
|
empty salt.
|
|
|
|
|
|
|
|
This class is implemented because Django used to store passwords this way
|
|
|
|
and to accept such password hashes. Some older Django installs still have
|
|
|
|
these values lingering around so we need to handle and upgrade them
|
|
|
|
properly.
|
2011-12-23 11:53:56 +08:00
|
|
|
"""
|
2012-03-01 04:12:16 +08:00
|
|
|
algorithm = "unsalted_md5"
|
2011-12-23 11:53:56 +08:00
|
|
|
|
|
|
|
def salt(self):
|
|
|
|
return ''
|
|
|
|
|
|
|
|
def encode(self, password, salt):
|
2013-02-26 03:01:57 +08:00
|
|
|
assert salt == ''
|
2012-08-29 02:59:56 +08:00
|
|
|
return hashlib.md5(force_bytes(password)).hexdigest()
|
2011-12-23 11:53:56 +08:00
|
|
|
|
|
|
|
def verify(self, password, encoded):
|
2013-02-02 18:57:25 +08:00
|
|
|
if len(encoded) == 37 and encoded.startswith('md5$$'):
|
|
|
|
encoded = encoded[5:]
|
2011-12-23 11:53:56 +08:00
|
|
|
encoded_2 = self.encode(password, '')
|
|
|
|
return constant_time_compare(encoded, encoded_2)
|
|
|
|
|
|
|
|
def safe_summary(self, encoded):
|
2013-08-03 13:41:15 +08:00
|
|
|
return OrderedDict([
|
2011-12-25 03:38:37 +08:00
|
|
|
(_('algorithm'), self.algorithm),
|
|
|
|
(_('hash'), mask_hash(encoded, show=3)),
|
2011-12-23 11:53:56 +08:00
|
|
|
])
|
|
|
|
|
|
|
|
|
|
|
|
class CryptPasswordHasher(BasePasswordHasher):
|
|
|
|
"""
|
|
|
|
Password hashing using UNIX crypt (not recommended)
|
|
|
|
|
|
|
|
The crypt module is not supported on all platforms.
|
|
|
|
"""
|
|
|
|
algorithm = "crypt"
|
|
|
|
library = "crypt"
|
|
|
|
|
|
|
|
def salt(self):
|
|
|
|
return get_random_string(2)
|
|
|
|
|
|
|
|
def encode(self, password, salt):
|
|
|
|
crypt = self._load_library()
|
|
|
|
assert len(salt) == 2
|
2012-12-22 23:00:15 +08:00
|
|
|
data = crypt.crypt(force_str(password), salt)
|
2011-12-23 11:53:56 +08:00
|
|
|
# we don't need to store the salt, but Django used to do this
|
|
|
|
return "%s$%s$%s" % (self.algorithm, '', data)
|
|
|
|
|
|
|
|
def verify(self, password, encoded):
|
|
|
|
crypt = self._load_library()
|
|
|
|
algorithm, salt, data = encoded.split('$', 2)
|
|
|
|
assert algorithm == self.algorithm
|
2012-12-22 23:00:15 +08:00
|
|
|
return constant_time_compare(data, crypt.crypt(force_str(password), data))
|
2011-12-23 11:53:56 +08:00
|
|
|
|
|
|
|
def safe_summary(self, encoded):
|
|
|
|
algorithm, salt, data = encoded.split('$', 2)
|
|
|
|
assert algorithm == self.algorithm
|
2013-08-03 13:41:15 +08:00
|
|
|
return OrderedDict([
|
2011-12-25 03:38:37 +08:00
|
|
|
(_('algorithm'), algorithm),
|
|
|
|
(_('salt'), salt),
|
|
|
|
(_('hash'), mask_hash(data, show=3)),
|
2011-12-23 11:53:56 +08:00
|
|
|
])
|