Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /Athroniaeth_keyshield /src /keyshield /hasher /argon2.py
| try: | |
| import argon2 # noqa: F401 | |
| except ModuleNotFoundError as e: | |
| raise ImportError("Argon2 backend requires 'argon2'. Install it with: uv add keyshield[argon2]") from e | |
| from typing import Optional | |
| from argon2 import PasswordHasher | |
| from argon2.exceptions import VerifyMismatchError, VerificationError, InvalidHashError | |
| from keyshield.hasher.base import BaseApiKeyHasher | |
| class Argon2ApiKeyHasher(BaseApiKeyHasher): | |
| """Argon2-based API key hasher and verifier with pepper.""" | |
| _pepper: str | |
| _ph: PasswordHasher | |
| def __init__( | |
| self, | |
| pepper: Optional[str] = None, | |
| password_hasher: Optional[PasswordHasher] = None, | |
| ) -> None: | |
| # Parameters by default are secure and recommended by Argon2 authors. | |
| # See https://argon2-cffi.readthedocs.io/en/stable/api.html | |
| self._ph = password_hasher or PasswordHasher() | |
| super().__init__(pepper=pepper) | |
| def _apply_pepper(self, api_key: str) -> str: | |
| return f"{api_key}{self._pepper}" | |
| def hash(self, key_secret: str) -> str: | |
| return self._ph.hash(self._apply_pepper(key_secret)) | |
| def verify(self, key_hash: str, key_secret: str) -> bool: | |
| try: | |
| return self._ph.verify( | |
| key_hash, | |
| self._apply_pepper(key_secret), | |
| ) | |
| except ( | |
| VerifyMismatchError, | |
| VerificationError, | |
| InvalidHashError, | |
| ): | |
| return False | |
Xet Storage Details
- Size:
- 1.46 kB
- Xet hash:
- decb0dfcb9a18e8a2638d6b35df3899a64c7164a39bd5f3bb90546f20ae95d01
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.