Return an instance of a loaded password hasher. Identify hasher algorithm by examining encoded hash, and call get_hasher() to return hasher. Raise ValueError if algorithm cannot be identified, or if hasher is not loaded.
(encoded)
| 171 | |
| 172 | |
| 173 | def identify_hasher(encoded): |
| 174 | """ |
| 175 | Return an instance of a loaded password hasher. |
| 176 | |
| 177 | Identify hasher algorithm by examining encoded hash, and call |
| 178 | get_hasher() to return hasher. Raise ValueError if |
| 179 | algorithm cannot be identified, or if hasher is not loaded. |
| 180 | """ |
| 181 | # Ancient versions of Django created plain MD5 passwords and accepted |
| 182 | # MD5 passwords with an empty salt. |
| 183 | if (len(encoded) == 32 and "$" not in encoded) or ( |
| 184 | len(encoded) == 37 and encoded.startswith("md5$$") |
| 185 | ): |
| 186 | algorithm = "unsalted_md5" |
| 187 | # Ancient versions of Django accepted SHA1 passwords with an empty salt. |
| 188 | elif len(encoded) == 46 and encoded.startswith("sha1$$"): |
| 189 | algorithm = "unsalted_sha1" |
| 190 | else: |
| 191 | algorithm = encoded.split("$", 1)[0] |
| 192 | return get_hasher(algorithm) |
| 193 | |
| 194 | |
| 195 | def mask_hash(hash, show=6, char="*"): |