Skip to content

Commit 28f3d54

Browse files
authored
fix: 🐛 add ENCRYPT_SUFFIX for inspect.getmodulename (#17 #45)
* fix: 🐛 add ENCRYPT_SUFFIX for `inspect.getmodulename` * perf: ⚡️ speed up decrypt_key
1 parent 7e08edb commit 28f3d54

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

pyencrypt/decrypt.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
from pathlib import Path
21
import os
2+
from functools import lru_cache
3+
from pathlib import Path
4+
35
from pyencrypt.aes import aes_decrypt
46
from pyencrypt.ntt import intt
57

68

9+
@lru_cache(maxsize=128)
710
def decrypt_key(cipher_key: str, d: int, n: int) -> str:
811
plain_ls = list()
912
for num in map(int, cipher_key.split("O")):

pyencrypt/loader.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import sys
33
import traceback
44
import types
5-
from importlib import abc
5+
from importlib import abc, machinery
66
from importlib._bootstrap_external import _NamespacePath
77
from importlib.machinery import ModuleSpec
88
from importlib.util import spec_from_loader
@@ -21,6 +21,9 @@ def __dir__(self) -> Iterable[str]:
2121
return []
2222

2323

24+
ENCRYPT_SUFFIX = ".pye"
25+
26+
2427
class EncryptFileLoader(abc.SourceLoader, Base):
2528
POSSIBLE_PATH = [
2629
Path(os.path.expanduser("~")) / ".licenses" / "license.lic",
@@ -79,12 +82,17 @@ def find_spec(
7982
) -> ModuleSpec:
8083
if path:
8184
if isinstance(path, _NamespacePath):
82-
file_path = Path(path._path[0]) / f'{fullname.rsplit(".", 1)[-1]}.pye'
85+
file_path = (
86+
Path(path._path[0])
87+
/ f'{fullname.rsplit(".", 1)[-1]}{ENCRYPT_SUFFIX}'
88+
)
8389
else:
84-
file_path = Path(path[0]) / f'{fullname.rsplit(".", 1)[-1]}.pye'
90+
file_path = (
91+
Path(path[0]) / f'{fullname.rsplit(".", 1)[-1]}{ENCRYPT_SUFFIX}'
92+
)
8593
else:
8694
for p in sys.path:
87-
file_path = Path(p) / f"{fullname}.pye"
95+
file_path = Path(p) / f"{fullname}{ENCRYPT_SUFFIX}"
8896
if file_path.exists():
8997
break
9098
file_path = file_path.absolute().as_posix()
@@ -99,4 +107,5 @@ def invalidate_caches(cls):
99107

100108

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

0 commit comments

Comments
 (0)