Source code for app.lib.crypt

from __future__ import annotations

import asyncio
import base64

from passlib.context import CryptContext

password_crypt_context = CryptContext(schemes=["argon2"], deprecated="auto")


[docs] def get_encryption_key(secret: str) -> bytes: """Get Encryption Key. Args: secret (str): Secret key used for encryption Returns: bytes: a URL safe encoded version of secret """ if len(secret) <= 32: secret = f"{secret:<32}"[:32] return base64.urlsafe_b64encode(secret.encode())
[docs] async def get_password_hash(password: str | bytes) -> str: """Get password hash. Args: password: Plain password Returns: str: Hashed password """ return await asyncio.get_running_loop().run_in_executor(None, password_crypt_context.hash, password)
[docs] async def verify_password(plain_password: str | bytes, hashed_password: str) -> bool: """Verify Password. Args: plain_password (str | bytes): The string or byte password hashed_password (str): the hash of the password Returns: bool: True if password matches hash. """ valid, _ = await asyncio.get_running_loop().run_in_executor( None, password_crypt_context.verify_and_update, plain_password, hashed_password, ) return bool(valid)