Return an instance of a loaded password hasher. If algorithm is 'default', return the default hasher. Lazily import hashers specified in the project's settings file if needed.
(algorithm="default")
| 146 | |
| 147 | |
| 148 | def get_hasher(algorithm="default"): |
| 149 | """ |
| 150 | Return an instance of a loaded password hasher. |
| 151 | |
| 152 | If algorithm is 'default', return the default hasher. Lazily import hashers |
| 153 | specified in the project's settings file if needed. |
| 154 | """ |
| 155 | if hasattr(algorithm, "algorithm"): |
| 156 | return algorithm |
| 157 | |
| 158 | elif algorithm == "default": |
| 159 | return get_hashers()[0] |
| 160 | |
| 161 | else: |
| 162 | hashers = get_hashers_by_algorithm() |
| 163 | try: |
| 164 | return hashers[algorithm] |
| 165 | except KeyError: |
| 166 | raise ValueError( |
| 167 | "Unknown password hashing algorithm '%s'. " |
| 168 | "Did you specify it in the PASSWORD_HASHERS " |
| 169 | "setting?" % algorithm |
| 170 | ) |
| 171 | |
| 172 | |
| 173 | def identify_hasher(encoded): |