Генератор временного архива лицензии

This commit is contained in:
2025-12-01 18:17:14 +07:00
parent c7256c1a54
commit 179743461f

View File

@@ -0,0 +1,43 @@
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