Ticket #4151: password2.diff

File password2.diff, 2.3 KB (added by Nick Efford <nick@…>, 17 years ago)

Updated diff to add support for stronger password hashes - replaces password.diff

  • django/conf/global_settings.py

     
    318318
    319319LOGIN_REDIRECT_URL = '/accounts/profile/'
    320320
     321# Hash algorithm that will be used by admin app if the
     322# hashlib module is available (Python 2.5 onwards).
     323# Allowed values are 'sha1', 'sha224', 'sha256', 'sha384'.
     324PASSWORD_HASH_ALGORITHM = 'sha256'
     325
    321326###########
    322327# TESTING #
    323328###########
  • django/contrib/auth/models.py

     
    1717    elif algo == 'sha1':
    1818        import sha
    1919        return hsh == sha.new(salt+raw_password).hexdigest()
     20    elif algo in ('sha224', 'sha256', 'sha384'):
     21        # Note: sha512 could be supported by making password
     22        # field of User model longer than 128 chars
     23        try:
     24            import hashlib
     25        except ImportError:
     26            # Python version is presumably earlier than 2.5
     27            raise ValueError, "%s not supported in this environment." % algo
     28        return hsh == hashlib.new(algo, salt+raw_password).hexdigest()
    2029    elif algo == 'crypt':
    2130        try:
    2231            import crypt
     
    149158        return full_name.strip()
    150159
    151160    def set_password(self, raw_password):
    152         import sha, random
    153         algo = 'sha1'
    154         salt = sha.new(str(random.random())).hexdigest()[:5]
    155         hsh = sha.new(salt+raw_password).hexdigest()
     161        import random
     162        try:
     163            import hashlib
     164            from django.conf import settings
     165            algo = settings.PASSWORD_HASH_ALGORITHM
     166            salt = hashlib.new(algo, str(random.random())).hexdigest()[:5]
     167            hsh = hashlib.new(algo, salt+raw_password).hexdigest()
     168        except ImportError:
     169            # Python version presumably earlier than 2.5,
     170            # so fall back on using SHA-1 hash
     171            import sha
     172            algo = 'sha1'
     173            salt = sha.new(str(random.random())).hexdigest()[:5]
     174            hsh = sha.new(salt+raw_password).hexdigest()
    156175        self.password = '%s$%s$%s' % (algo, salt, hsh)
    157176
    158177    def check_password(self, raw_password):
Back to Top