Download Backend/helper/passwords.py from DevDoCode/stremio: direct link, hf CLI and curl.
- Browser
- Download file 919 Bytes
-
https://huggingface.co/spaces/DevDoCode/stremio/resolve/main/Backend/helper/passwords.py
- Command line
-
hf download hf://spaces/DevDoCode/stremio/Backend/helper/passwords.py
-
curl -L -o passwords.py https://huggingface.co/spaces/DevDoCode/stremio/resolve/main/Backend/helper/passwords.py
919 Bytes
| import hashlib | |
| import hmac | |
| import secrets | |
| _ALGO = "pbkdf2_sha256" | |
| _ITERATIONS = 200_000 | |
| def is_hashed(stored: str) -> bool: | |
| return isinstance(stored, str) and stored.startswith(f"{_ALGO}$") | |
| def hash_password(password: str) -> str: | |
| salt = secrets.token_bytes(16) | |
| digest = hashlib.pbkdf2_hmac("sha256", (password or "").encode(), salt, _ITERATIONS) | |
| return f"{_ALGO}${_ITERATIONS}${salt.hex()}${digest.hex()}" | |
| def verify_password(password: str, stored: str) -> bool: | |
| if not stored: | |
| return False | |
| if not is_hashed(stored): | |
| return hmac.compare_digest(password or "", stored) | |
| try: | |
| _, iterations, salt_hex, hash_hex = stored.split("$") | |
| digest = hashlib.pbkdf2_hmac("sha256", (password or "").encode(), bytes.fromhex(salt_hex), int(iterations)) | |
| return hmac.compare_digest(digest.hex(), hash_hex) | |
| except (ValueError, TypeError): | |
| return False | |