diff --git a/django/contrib/auth/hashers.py b/django/contrib/auth/hashers.py
index bd760cd..27201a7 100644
a
|
b
|
def identify_hasher(encoded):
|
123 | 123 | """ |
124 | 124 | Returns an instance of a loaded password hasher. |
125 | 125 | |
126 | | Identifies hasher algorithm by examining encoded hash, and calls |
127 | | get_hasher() to return hasher. Raises ValueError if |
128 | | algorithm cannot be identified, or if hasher is not loaded. |
| 126 | Identifies hasher algorithm by asking each hasher in turn if they are |
| 127 | accepting the encoded hash format. Raises ValueError if |
| 128 | algorithm cannot be identified. |
129 | 129 | """ |
130 | | if ((len(encoded) == 32 and '$' not in encoded) or |
131 | | (len(encoded) == 37 and encoded.startswith('md5$$'))): |
132 | | algorithm = 'unsalted_md5' |
133 | | else: |
134 | | algorithm = encoded.split('$', 1)[0] |
135 | | return get_hasher(algorithm) |
| 130 | if HASHERS is None: |
| 131 | load_hashers() |
| 132 | for algorithm, hasher in HASHERS.items(): |
| 133 | if hasher.supports(encoded): |
| 134 | return hasher |
| 135 | raise ValueError( |
| 136 | "Unable to find any hasher accepting a hash starting with '%s...'" % ( |
| 137 | encoded[:12],)) |
136 | 138 | |
137 | 139 | |
138 | 140 | def mask_hash(hash, show=6, char="*"): |
… |
… |
class BasePasswordHasher(object):
|
172 | 174 | raise ValueError("Hasher '%s' doesn't specify a library attribute" % |
173 | 175 | self.__class__) |
174 | 176 | |
| 177 | def supports(self, hash_): |
| 178 | """ |
| 179 | Return True if hash_ is supported by this hasher. |
| 180 | """ |
| 181 | return self.algorithm == hash_.split('$', 1)[0] |
| 182 | |
175 | 183 | def salt(self): |
176 | 184 | """ |
177 | 185 | Generates a cryptographically secure nonce salt in ascii |
… |
… |
class UnsaltedMD5PasswordHasher(BasePasswordHasher):
|
361 | 369 | """ |
362 | 370 | algorithm = "unsalted_md5" |
363 | 371 | |
| 372 | def supports(self, hash_): |
| 373 | return (len(hash_) == 32 and '$' not in hash_) or \ |
| 374 | (len(hash_) == 37 and hash_.startswith('md5$$')) |
| 375 | |
364 | 376 | def salt(self): |
365 | 377 | return '' |
366 | 378 | |