Last active
November 22, 2025 18:13
-
-
Save pmoust/ad9ef7e26a5d0a7a886ef24e21274dfc to your computer and use it in GitHub Desktop.
A Django hasher to be used only in CI - in a realworld marketplace application running ~2k tests, it reduced test time by 40%
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| No-op password hasher for tests that performs absolutely no encryption. | |
| This hasher stores passwords in plain text (with a simple prefix to identify them) | |
| to maximize test performance. It should ONLY be used during tests. | |
| """ | |
| from django.contrib.auth.hashers import BasePasswordHasher | |
| class NoOpPasswordHasher(BasePasswordHasher): | |
| """ | |
| Password hasher that does absolutely no encryption. | |
| This hasher stores passwords as-is with a simple prefix for identification. | |
| It's designed for maximum test performance and should NEVER be used in production. | |
| """ | |
| algorithm = "noop" | |
| def encode(self, password, salt=None): | |
| """ | |
| Encode password with no encryption - just add a prefix. | |
| Args: | |
| password: The plain text password | |
| salt: Ignored (not used) | |
| Returns: | |
| String in format: noop$password | |
| """ | |
| return f"noop${password}" | |
| def verify(self, password, encoded): | |
| """ | |
| Verify password by simple string comparison. | |
| Args: | |
| password: The plain text password to verify | |
| encoded: The encoded password (format: noop$password) | |
| Returns: | |
| True if passwords match, False otherwise | |
| """ | |
| if not encoded.startswith("noop$"): | |
| return False | |
| stored_password = encoded[5:] # Remove "noop$" prefix | |
| return password == stored_password | |
| def safe_summary(self, encoded): | |
| """ | |
| Return a summary of the encoded password. | |
| Args: | |
| encoded: The encoded password | |
| Returns: | |
| Dictionary with algorithm and raw password (for debugging) | |
| """ | |
| if not encoded.startswith("noop$"): | |
| raise ValueError("Invalid encoded password format") | |
| return { | |
| "algorithm": self.algorithm, | |
| "password": encoded[5:], # Show the plain password for debugging | |
| } | |
| def must_update(self, encoded): | |
| """ | |
| Return False - no updates needed for this hasher. | |
| Args: | |
| encoded: The encoded password | |
| Returns: | |
| False (passwords never need updating with this hasher) | |
| """ | |
| return False |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In
settings.py