Spaces:
Sleeping
Sleeping
Download utils/security.py from ByteRiot/CandidateExplorer: direct link, hf CLI and curl.
- Browser
- Download file 901 Bytes
-
https://huggingface.co/spaces/ByteRiot/CandidateExplorer/resolve/main/utils/security.py
- Command line
-
hf download hf://spaces/ByteRiot/CandidateExplorer/utils/security.py
-
curl -L -o security.py https://huggingface.co/spaces/ByteRiot/CandidateExplorer/resolve/main/utils/security.py
901 Bytes
| import hashlib | |
| from config.constant import SecurityConstants | |
| from passlib.hash import bcrypt | |
| def _prepare_password(password: str) -> bytes: | |
| password_bytes = password.encode("utf-8") | |
| # SHA-256 = 32 bytes (ALWAYS < 72) | |
| digest = hashlib.sha256(password_bytes).digest() | |
| # This slice is technically redundant, but explicit | |
| return digest[:SecurityConstants.BCRYPT_MAX_BYTES] | |
| def hash_password(password: str) -> str: | |
| prepared = _prepare_password(password) | |
| # ASSERTION (fail fast if something is wrong) | |
| assert len(prepared) <= 72, f"bcrypt input too long: {len(prepared)} bytes" | |
| return bcrypt.hash(prepared) | |
| def verify_password(plain_password: str, hashed_password: str) -> bool: | |
| prepared = _prepare_password(plain_password) | |
| assert len(prepared) <= 72, f"bcrypt input too long: {len(prepared)} bytes" | |
| return bcrypt.verify(prepared, hashed_password) | |