Ticket #13969: better_salting.diff
File better_salting.diff, 1.5 KB (added by , 14 years ago) |
---|
-
django/contrib/auth/models.py
32 32 return sha_constructor(salt + raw_password).hexdigest() 33 33 raise ValueError("Got unknown password algorithm type in password.") 34 34 35 def gen_salt(length=12): 36 """ 37 Returns a random string of length characters from the set of a-z, A-Z, 0-9 38 for use as a salt. 39 40 The default length of 12 with the a-z, A-Z, 0-9 character set returns a 41 71-bit salt. log_2((26+26+10)^12) =~ 71 bits 42 """ 43 allowed_chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789' 44 45 import random 46 try: 47 random = random.SystemRandom() 48 except NotImplementedError: 49 import random 50 51 ret = [] 52 for i in xrange(length): 53 ret.append(random.choice(allowed_chars)) 54 55 return ''.join(ret) 56 35 57 def check_password(raw_password, enc_password): 36 58 """ 37 59 Returns a boolean of whether the raw_password was correct. Handles … … 238 260 return full_name.strip() 239 261 240 262 def set_password(self, raw_password): 241 import random242 263 algo = 'sha1' 243 salt = ge t_hexdigest(algo, str(random.random()), str(random.random()))[:5]264 salt = gen_salt() 244 265 hsh = get_hexdigest(algo, salt, raw_password) 245 266 self.password = '%s$%s$%s' % (algo, salt, hsh) 246 267