Skip to content

Commit

Permalink
fix: 🐛 add ENCRYPT_SUFFIX for inspect.getmodulename (#17 #45)
Browse files Browse the repository at this point in the history
* fix: 🐛 add ENCRYPT_SUFFIX for `inspect.getmodulename`

* perf: ⚡️ speed up decrypt_key
  • Loading branch information
ZhaoQi99 authored Nov 5, 2024
1 parent 7e08edb commit 28f3d54
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
5 changes: 4 additions & 1 deletion pyencrypt/decrypt.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
from pathlib import Path
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
17 changes: 13 additions & 4 deletions pyencrypt/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import sys
import traceback
import types
from importlib import abc
from importlib import abc, machinery
from importlib._bootstrap_external import _NamespacePath
from importlib.machinery import ModuleSpec
from importlib.util import spec_from_loader
Expand All @@ -21,6 +21,9 @@ def __dir__(self) -> Iterable[str]:
return []


ENCRYPT_SUFFIX = ".pye"


class EncryptFileLoader(abc.SourceLoader, Base):
POSSIBLE_PATH = [
Path(os.path.expanduser("~")) / ".licenses" / "license.lic",
Expand Down Expand Up @@ -79,12 +82,17 @@ def find_spec(
) -> ModuleSpec:
if path:
if isinstance(path, _NamespacePath):
file_path = Path(path._path[0]) / f'{fullname.rsplit(".", 1)[-1]}.pye'
file_path = (
Path(path._path[0])
/ f'{fullname.rsplit(".", 1)[-1]}{ENCRYPT_SUFFIX}'
)
else:
file_path = Path(path[0]) / f'{fullname.rsplit(".", 1)[-1]}.pye'
file_path = (
Path(path[0]) / f'{fullname.rsplit(".", 1)[-1]}{ENCRYPT_SUFFIX}'
)
else:
for p in sys.path:
file_path = Path(p) / f"{fullname}.pye"
file_path = Path(p) / f"{fullname}{ENCRYPT_SUFFIX}"
if file_path.exists():
break
file_path = file_path.absolute().as_posix()
Expand All @@ -99,4 +107,5 @@ def invalidate_caches(cls):


# TODO: generate randomly AES Class
machinery.EXTENSION_SUFFIXES.append(ENCRYPT_SUFFIX)
sys.meta_path.insert(0, EncryptFileFinder)

0 comments on commit 28f3d54

Please sign in to comment.