Skip to content

Commit

Permalink
perf: ⚡️ speed up decrypt_key
Browse files Browse the repository at this point in the history
Closes: #17
  • Loading branch information
ZhaoQi99 committed Nov 29, 2024
1 parent deda1ed commit aa52595
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
2 changes: 0 additions & 2 deletions pyencrypt/decrypt.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import os
from functools import lru_cache
from pathlib import Path

from pyencrypt.aes import aes_decrypt
from pyencrypt.ntt import intt


@lru_cache(maxsize=128)
def decrypt_key(cipher_key: str, d: int, n: int) -> str:
plain_ls = list()
for num in map(int, cipher_key.split("O")):
Expand Down
10 changes: 8 additions & 2 deletions pyencrypt/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import sys
import traceback
import types
from functools import lru_cache
from importlib import abc, machinery
from importlib._bootstrap_external import _NamespacePath
from importlib.machinery import ModuleSpec
Expand Down Expand Up @@ -49,6 +50,11 @@ def _init_license_path(self) -> None:
self.license_path = path
break

@classmethod
@lru_cache(maxsize=128)
def _decrypt_key(cls, cipher_key: str, d: int, n: int):
return decrypt_key(cipher_key, d, n)

def check(self) -> bool:
if self.license is False:
return False
Expand All @@ -58,7 +64,7 @@ def check(self) -> bool:

__n, __d = self.__private_key.split("O", 1)
check_license(
self.license_path, decrypt_key(self.__cipher_key, int(__d), int(__n))
self.license_path, self._decrypt_key(self.__cipher_key, int(__d), int(__n))
)
return True

Expand All @@ -72,7 +78,7 @@ def get_data(self, path: _Path) -> bytes:
try:
__n, __d = self.__private_key.split("O", 1)
return decrypt_file(
Path(path), decrypt_key(self.__cipher_key, int(__d), int(__n))
Path(path), self._decrypt_key(self.__cipher_key, int(__d), int(__n))
)
except Exception:
traceback.print_exc()
Expand Down

0 comments on commit aa52595

Please sign in to comment.