import tempfile from zipfile import ZipFile from pathlib import Path from ..utils.encoding import variant_base64_encode from ..utils.crypto import encrypt_bytes class LicenseType: Professional: int = 1 Educational: int = 3 Personal: int = 4 def generate_license_file( user_name: str, version: str, lic_type: int = LicenseType.Professional, count: int = 1 ) -> tuple[str, tempfile.TemporaryDirectory]: """ Генерация временного ZIP-файла с лицензией Custom.mxtpro. Возвращает путь к файлу и объект временной директории для cleanup. """ # Разбор версии try: major_str, minor_str = version.split(".") major_version: int = int(major_str) minor_version: int = int(minor_str) except Exception as e: raise ValueError(f"Неверный формат версии: {version}") from e # Формирование строки лицензии license_str: str = ( f"{lic_type}#{user_name}|{major_version}{minor_version}" f"#{count}#{major_version}3{minor_version}6{minor_version}#0#0#0#" ) # Шифрование и кодирование encrypted: bytes = encrypt_bytes(0x0787, license_str.encode("utf-8")) encoded: str = variant_base64_encode(encrypted).decode("ascii") # Создание временной директории и файла с фиксированным именем temp_dir = tempfile.TemporaryDirectory() filepath = Path(temp_dir.name) / "Custom.mxtpro" with ZipFile(filepath, "w") as zf: zf.writestr("Pro.key", encoded) return str(filepath), temp_dir