Ticket #5787: django-bcrypt.diff
File django-bcrypt.diff, 6.7 KB (added by , 17 years ago) |
---|
-
django/conf/global_settings.py
333 333 334 334 LOGIN_REDIRECT_URL = '/accounts/profile/' 335 335 336 # The preferred hash algorithm for storing passwords 337 # Acceptable values are 'crypt', 'md5', 'sha1', and 'bcrypt' 338 # NOTE : 'crypt' and 'md5' are provided only for legacy integration 339 PREFERRED_HASH = 'sha1' 340 341 # The number of rounds determines the complexity of the bcrypt alg. 342 # The work factor is 2**log_rounds, and the default is 12 343 BCRYPT_LOG_ROUNDS = 12 344 336 345 ########### 337 346 # TESTING # 338 347 ########### -
django/contrib/auth/models.py
46 46 return hashlib.sha1(salt + raw_password).hexdigest() 47 47 raise ValueError("Got unknown password algorithm type in password.") 48 48 49 def hash_password(raw_password, enc_password): 50 """ 51 Returns a formatted hash of the user's password. 52 """ 53 if enc_password in ('crypt', 'md5', 'sha1', 'bcrypt'): 54 validating = False 55 algo = enc_password 56 algo_args = '' 57 hsh = '' 58 else: 59 validating = True 60 if enc_password[0] == '$': 61 algo, algo_args, hsh = enc_password[1:].split('$') 62 else: 63 algo, algo_args, hsh = enc_password.split('$') 64 if algo == 'bcrypt' or algo == '2' or algo == '2a': 65 try: 66 import bcrypt 67 if validating: 68 # If we are validating, use the hash in the provided password 69 salt = enc_password 70 else: 71 # If we are generating a new hash, we need to generate a salt 72 from django.conf import settings 73 salt = bcrypt.gensalt(settings.BCRYPT_LOG_ROUNDS) 74 return bcrypt.hashpw(raw_password, salt) 75 except ImportError: 76 raise ValueError('"bcrypt" password algorithm not supported in this environment') 77 else: 78 if validating: 79 salt = algo_args 80 else: 81 import random 82 salt = get_hexdigest(algo, str(random.random()), str(random.random()))[:5] 83 hsh = get_hexdigest(algo, salt, raw_password) 84 return '%s$%s$%s' % (algo, salt, hsh) 85 49 86 def check_password(raw_password, enc_password): 50 87 """ 51 88 Returns a boolean of whether the raw_password was correct. Handles 52 89 encryption formats behind the scenes. 53 90 """ 54 algo, salt, hsh = enc_password.split('$') 55 return hsh == get_hexdigest(algo, salt, raw_password) 91 return enc_password == hash_password(raw_password, enc_password) 56 92 57 93 class SiteProfileNotAvailable(Exception): 58 94 pass … … 181 217 return full_name.strip() 182 218 183 219 def set_password(self, raw_password): 184 import random 185 algo = 'sha1' 186 salt = get_hexdigest(algo, str(random.random()), str(random.random()))[:5] 187 hsh = get_hexdigest(algo, salt, raw_password) 188 self.password = '%s$%s$%s' % (algo, salt, hsh) 220 """ 221 Sets the users's password hash to a hash of raw_password. Handles 222 encryption formats behind the scenes. 223 """ 224 from django.conf import settings 225 algo = settings.PREFERRED_HASH 226 self.password = hash_password(raw_password, algo) 189 227 190 228 def check_password(self, raw_password): 191 229 """ -
docs/settings.txt
225 225 ``CommonMiddleware`` is installed (see the `middleware docs`_). See also 226 226 ``PREPEND_WWW``. 227 227 228 BCRYPT_LOG_ROUNDS 229 ------------- 230 231 **New in Django development version** 232 233 Default: ``12`` 234 235 The number of rounds determines the complexity of the ``bcrypt`` password 236 hashing algorithm. The work factor is 2**BCRYPT_LOG_ROUNDS. 237 228 238 CACHE_BACKEND 229 239 ------------- 230 240 … … 678 688 See `allowed date format strings`_. See also ``DATE_FORMAT``, 679 689 ``DATETIME_FORMAT``, ``TIME_FORMAT`` and ``YEAR_MONTH_FORMAT``. 680 690 691 PREFERRED_HASH 692 ---------- 693 694 **New in Django development version** 695 696 Default: ``'sha1'`` 697 698 The default hash to use when saving new passwords. The hashtype is either 699 ``sha1`` (default), ``md5``, ``crypt`` or ``bcrypt`` -- the algorithm used to 700 perform a one-way hash of the password. 701 702 Note that the ``crypt`` method is only supported on platforms that have the 703 standard Python ``crypt`` module available, and ``crypt`` support is only 704 available in the Django development version. Likewise the ``bcrypt`` algorithm 705 is supported only on platforms that have the standard Python ``bcrypt`` module 706 available, and like ``crypt`` it is only supported in the development version. 707 708 **Important Note** : ``md5`` and ``crypt`` are both deprecated algorithms that 709 are maintained for legacy integration. New applications are recommended to use 710 stronger algorithms like ``sha1`` (default) or ``bcrypt``. 711 681 712 PREPEND_WWW 682 713 ----------- 683 714 -
docs/authentication.txt
218 218 219 219 That's hashtype, salt and hash, separated by the dollar-sign character. 220 220 221 Hashtype is either ``sha1`` (default), ``md5`` or ``crypt`` -- the algorithm 222 used to perform a one-way hash of the password. Salt is a random string used 223 to salt the raw password to create the hash. Note that the ``crypt`` method is 224 only supported on platforms that have the standard Python ``crypt`` module 225 available, and ``crypt`` support is only available in the Django development 226 version. 221 Hashtype is either ``sha1`` (default), ``md5``, ``crypt`` or ``bcrypt`` -- the 222 algorithm used to perform a one-way hash of the password. Salt is a random 223 string used to salt the raw password to create the hash. Note that the ``crypt`` 224 method is only supported on platforms that have the standard Python ``crypt`` 225 module available, and ``crypt`` support is only available in the Django 226 development version. Likewise the ``bcrypt`` algorithm is supported only on 227 platforms that have the standard Python ``bcrypt`` module available, and like 228 ``crypt`` it is only supported in the development version. 227 229 230 You may change the default hash algorithm by changing the PREFERRED_HASH 231 setting. Please note that ``md5`` and ``crypt`` are both deprecated algorithms 232 that are maintained for legacy integration. New applications are recommended to 233 use stronger algorithms like ``sha1`` (default) or ``bcrypt``. 234 228 235 For example:: 229 236 230 237 sha1$a1976$a36cc8cbf81742a8fb52e221aaeab48ed7f58ab4