Skip to content

Commit b5469e0

Browse files
committed
style: 💄 format code using yapf
1 parent 15fe9d5 commit b5469e0

File tree

7 files changed

+46
-57
lines changed

7 files changed

+46
-57
lines changed

pyencrypt/decrypt.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,7 @@ def _decrypt_file(data: bytes, key: str) -> bytes:
1616
return aes_decrypt(data, key)
1717

1818

19-
def decrypt_file(path: Path,
20-
key: str,
21-
delete_origin: bool = False,
22-
new_path: Path = None) -> bytes:
19+
def decrypt_file(path: Path, key: str, delete_origin: bool = False, new_path: Path = None) -> bytes:
2320
if path.suffix != '.pye':
2421
raise Exception(f"{path.name} can't be decrypted.")
2522
data = _decrypt_file(path.read_bytes(), key)

pyencrypt/encrypt.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,15 @@ def generate_so_file(cipher_key: str, d: int, n: int, base_dir: Path = None):
5252
need_import_files = ['ntt.py', 'aes.py', 'decrypt.py']
5353
for file in need_import_files:
5454
file_path = path / file
55-
decrypt_source_ls.append(file_path.read_text().replace(
56-
'from pyencrypt.ntt import intt',
57-
'').replace('from pyencrypt.aes import aes_decrypt', ''))
55+
decrypt_source_ls.append(
56+
file_path.read_text().replace('from pyencrypt.ntt import intt',
57+
'').replace('from pyencrypt.aes import aes_decrypt', '')
58+
)
5859

5960
loader_source_path = path / 'loader.py'
6061
loader_source = loader_source_path.read_text().replace(
61-
"__private_key = ''", f"__private_key = '{private_key}'",
62-
1).replace("__cipher_key = ''", f"__cipher_key = '{cipher_key}'",
63-
1).replace("from pyencrypt.decrypt import *", '')
62+
"__private_key = ''", f"__private_key = '{private_key}'", 1
63+
).replace("__cipher_key = ''", f"__cipher_key = '{cipher_key}'", 1).replace("from pyencrypt.decrypt import *", '')
6464

6565
if base_dir is None:
6666
base_dir = Path(os.getcwd())
@@ -79,10 +79,14 @@ def generate_so_file(cipher_key: str, d: int, n: int, base_dir: Path = None):
7979
loader_origin_file_path.write_text(f"{decrypt_source}\n{loader_source}")
8080

8181
args = [
82-
'pyminifier', '--obfuscate-classes', '--obfuscate-import-methods',
83-
'--replacement-length', '20', '-o',
82+
'pyminifier',
83+
'--obfuscate-classes',
84+
'--obfuscate-import-methods',
85+
'--replacement-length',
86+
'20',
87+
'-o',
88+
loader_file_path.as_posix(),
8489
loader_file_path.as_posix(),
85-
loader_file_path.as_posix()
8690
]
8791
ret = subprocess.run(args, shell=False, encoding='utf-8')
8892
if ret.returncode == 0:
@@ -100,10 +104,7 @@ def generate_so_file(cipher_key: str, d: int, n: int, base_dir: Path = None):
100104
return True
101105

102106

103-
def encrypt_file(path: Path,
104-
key: str,
105-
delete_origin: bool = False,
106-
new_path: Path = None):
107+
def encrypt_file(path: Path, key: str, delete_origin: bool = False, new_path: Path = None):
107108
if not can_encrypt(path):
108109
raise Exception(f"{path.name} can't be encrypted.")
109110
encrypted_data = _encrypt_file(path.read_bytes(), key)

pyencrypt/generate.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ def generate_aes_key(size: int = 32) -> bytes:
88
return base64.b64encode(os.urandom(size))
99

1010

11-
def generate_rsa_number(bits: int)-> dict:
11+
def generate_rsa_number(bits: int) -> dict:
1212
r = RSA.generate(bits)
1313
return {'p': r.p, 'q': r.q, 'n': r.n, 'e': r.e, 'd': r.d}
14-

pyencrypt/loader.py

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,17 @@ def get_filename(self, fullname: str) -> str:
3232
def get_data(self, path: _Path) -> bytes:
3333
try:
3434
__n, __d = self.__private_key.split('O', 1)
35-
return decrypt_file(
36-
Path(path), decrypt_key(self.__cipher_key, int(__d), int(__n)))
35+
return decrypt_file(Path(path), decrypt_key(self.__cipher_key, int(__d), int(__n)))
3736
except Exception:
3837
traceback.print_exc()
3938
return b''
4039

4140

4241
class EncryptFileFinder(abc.MetaPathFinder, Base):
43-
def find_spec(self,
44-
fullname: str,
45-
path: Sequence[_Path],
46-
target: types.ModuleType = None) -> ModuleSpec:
42+
def find_spec(self, fullname: str, path: Sequence[_Path], target: types.ModuleType = None) -> ModuleSpec:
4743
if path:
4844
if isinstance(path, _NamespacePath):
49-
file_path = Path(
50-
path._path[0]) / f'{fullname.rsplit(".",1)[-1]}.pye'
45+
file_path = Path(path._path[0]) / f'{fullname.rsplit(".",1)[-1]}.pye'
5146
else:
5247
file_path = Path(path[0]) / f'{fullname.rsplit(".",1)[-1]}.pye'
5348
else:
@@ -59,9 +54,7 @@ def find_spec(self,
5954
if not os.path.exists(file_path):
6055
return None
6156
loader = EncryptFileLoader(file_path)
62-
return spec_from_loader(name=fullname,
63-
loader=loader,
64-
origin='origin-encrypt')
57+
return spec_from_loader(name=fullname, loader=loader, origin='origin-encrypt')
6558

6659

6760
# TODO: generate randomly AES Class

tests/test_aes.py

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,30 @@
1414
PLAIN_3 = b'abcdefghijklmnop'
1515
CIPHER_3 = b'r\x14\xa7\x92\xd6\x1f\x0c\xf4\x10g\x99\t/\xf0z\xfc' + b'&\x80\xdb\x94\xd1\xf7\x9f\xe0Qo\x05\x98\x7f\xe6j\x8c'
1616

17-
class TestAES:
1817

19-
@pytest.mark.parametrize('key, plain, cipher', [
20-
(AES_KEY, PLAIN_1, CIPHER_1),
21-
(AES_KEY, PLAIN_2, CIPHER_2),
22-
(AES_KEY, PLAIN_3, CIPHER_3),
23-
])
18+
class TestAES:
19+
@pytest.mark.parametrize(
20+
'key, plain, cipher', [
21+
(AES_KEY, PLAIN_1, CIPHER_1),
22+
(AES_KEY, PLAIN_2, CIPHER_2),
23+
(AES_KEY, PLAIN_3, CIPHER_3),
24+
]
25+
)
2426
def test_aes_encrypt(self, plain, cipher, key):
2527
assert aes_encrypt(plain, key) == cipher
2628

27-
@pytest.mark.parametrize('key, plain, cipher', [
28-
(AES_KEY, PLAIN_1, CIPHER_1),
29-
(AES_KEY, PLAIN_2, CIPHER_2),
30-
(AES_KEY, PLAIN_3, CIPHER_3),
31-
])
29+
@pytest.mark.parametrize(
30+
'key, plain, cipher', [
31+
(AES_KEY, PLAIN_1, CIPHER_1),
32+
(AES_KEY, PLAIN_2, CIPHER_2),
33+
(AES_KEY, PLAIN_3, CIPHER_3),
34+
]
35+
)
3236
def test_aes_decrypt(self, plain, cipher, key):
3337
assert aes_decrypt(cipher, key) == plain
3438

3539

3640
class TestAESModeOfOperationECB:
37-
3841
def setup_class(self):
3942
self.cipher = AESModeOfOperationECB(AES_KEY)
4043

@@ -54,12 +57,12 @@ def test_decrypt_exception(self, length):
5457
(PLAIN_PADDING_1, CIPHER_1),
5558
(PLAIN_PADDING_2, CIPHER_2),
5659
])
57-
def test_encrypt(self,plain, cipher):
60+
def test_encrypt(self, plain, cipher):
5861
assert self.cipher.encrypt(plain) == cipher
5962

6063
@pytest.mark.parametrize('plain, cipher', [
6164
(PLAIN_PADDING_1, CIPHER_1),
6265
(PLAIN_PADDING_2, CIPHER_2),
6366
])
64-
def test_decrypt(self,plain, cipher):
65-
assert self.cipher.decrypt(cipher) == plain
67+
def test_decrypt(self, plain, cipher):
68+
assert self.cipher.decrypt(cipher) == plain

tests/test_encrypt.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
from pyencrypt.generate import generate_aes_key
99

1010

11-
1211
@pytest.mark.parametrize('key', [
1312
AES_KEY,
1413
generate_aes_key(),
@@ -34,7 +33,6 @@ def test_can_encrypt(path, expected):
3433

3534

3635
class TestGenarateSoFile:
37-
3836
def setup_method(self, method):
3937
if method.__name__ == 'test_generate_so_file_default_path':
4038
shutil.rmtree((Path(os.getcwd()) / 'encrypted').as_posix(), ignore_errors=True)
@@ -100,8 +98,9 @@ def test_encrypt_file_new_path(python_file_path):
10098
assert new_path.exists() == True
10199
assert python_file_path.exists() == True
102100

101+
103102
def test_encrypt_file_new_path_exception(python_file_path):
104103
new_path = python_file_path.parent / 'test.py'
105104
with pytest.raises(Exception) as excinfo:
106105
encrypt_file(python_file_path, AES_KEY, new_path=new_path)
107-
assert str(excinfo.value) == 'Encrypted file path must be pye suffix.'
106+
assert str(excinfo.value) == 'Encrypted file path must be pye suffix.'

tests/test_ntt.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66

77
class TestNtt:
8-
98
@pytest.mark.parametrize(
109
"input,expected", [
1110
([1, 2, 3, 4], [10, 173167434, 998244351, 825076915]),
@@ -29,7 +28,7 @@ def test_ntt_inverse(self, input, expected):
2928
random_list(6),
3029
random_list(7),
3130
])
32-
def test_ntt_exception(self,input):
31+
def test_ntt_exception(self, input):
3332
with pytest.raises(ValueError) as excinfo:
3433
ntt(input)
3534
assert str(excinfo.value) == "The length of input must be a power of 2."
@@ -44,13 +43,11 @@ def test_intt_exception(self, input):
4443
intt(input)
4544
assert str(excinfo.value) == "The length of input must be a power of 2."
4645

47-
@pytest.mark.parametrize(
48-
"input,expected", [
49-
(random_list(4), 4),
50-
(random_list(8), 8),
51-
(random_list(16), 16),
52-
]
53-
)
46+
@pytest.mark.parametrize("input,expected", [
47+
(random_list(4), 4),
48+
(random_list(8), 8),
49+
(random_list(16), 16),
50+
])
5451
def test_ntt_result_length(self, input, expected):
5552
assert len(ntt(input)) == expected
5653

0 commit comments

Comments
 (0)