import base64
import os

from typing import (Literal, overload,)

class PasswordUtils:
  @overload
  @classmethod
  def encrypt(
    cls,
    secret: str,
    mode: Literal['base64'],
  ) -> tuple[str, str]: ...

  @overload
  @classmethod
  def encrypt(
    cls,
    secret: str,
    mode: Literal['bytes'],
  ) -> tuple[bytes, bytes]: ...

  @classmethod
  def encrypt(
    cls,
    secret: str,
    mode: Literal['bytes', 'base64'],
  ) -> tuple[str, str] | tuple[bytes, bytes]:
    from cryptography.hazmat.primitives.kdf.scrypt import Scrypt

    salt = os.urandom(16)

    # derive
    kdf = Scrypt(
      salt=salt,
      length=32,
      n=2**14,
      r=8,
      p=1,
    )

    hashed_secret = kdf.derive(secret.encode('utf-8'))

    if mode == 'bytes':
      return (salt, hashed_secret)
    elif mode == 'base64':
      res_tuple = tuple((
        base64.b64encode(o, width=0).decode('utf-8')
        for o in (salt, hashed_secret,)
      ))
      return (res_tuple[0], res_tuple[1])
    else:
      raise NotImplementedError

    # # verify
    # kdf = Scrypt(
    #   salt=salt,
    #   length=32,
    #   n=2**14,
    #   r=8,
    #   p=1,
    # )
    #
    # kdf.verify(b"my great password", key)
    #
