rtzip 0.5.0__tar.gz → 0.5.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: rtzip
3
- Version: 0.5.0
3
+ Version: 0.5.1
4
4
  Requires-Dist: obstruct==0.5.2
5
5
  Requires-Dist: pyaes>=1.6.1
6
6
  Requires-Python: >=3.12
@@ -4,7 +4,7 @@ requires = ["uv_build < 0.13"]
4
4
 
5
5
  [project]
6
6
  name = "rtzip"
7
- version = "0.5.0"
7
+ version = "0.5.1"
8
8
  requires-python = ">=3.12"
9
9
  dependencies = [
10
10
  "obstruct==0.5.2",
@@ -6,7 +6,7 @@ requires = [
6
6
 
7
7
  [project]
8
8
  name = "rtzip"
9
- version = "0.5.0"
9
+ version = "0.5.1"
10
10
  requires-python = ">=3.12"
11
11
  dependencies = [
12
12
  "obstruct==0.5.2",
@@ -271,7 +271,7 @@ class RemoteZip:
271
271
  entry = entry or self.find_file_entry(normpath(bytes(filename)))
272
272
 
273
273
  if reuse and entry.filename in self.cached_headers:
274
- return self.cached_headers[entry.filename]
274
+ return self.cached_headers[entry.filename], b''
275
275
 
276
276
  buffer = await self._read_range(entry.local_header_offset, entry.__cstruct__.size + 64) # 预留64字节的变长字段
277
277
 
@@ -0,0 +1,78 @@
1
+ # Written by DeepSeek
2
+ import itertools
3
+ import warnings
4
+
5
+ from cryptography.hazmat.primitives import hashes
6
+ from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
7
+ from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
8
+
9
+
10
+ def pbkdf2_hmac_impl(pwd: bytes, length: int, salt: bytes, iterations: int) -> bytes:
11
+ kdf = PBKDF2HMAC(
12
+ algorithm=hashes.SHA1(),
13
+ length=length,
14
+ salt=salt,
15
+ iterations=iterations,
16
+ )
17
+ return kdf.derive(pwd) # 注意:密码需要单独传递
18
+
19
+
20
+ class CryptographyAESCipher:
21
+ __slots__ = ('_d', '_enc_key', '_initial_value', '_c', '_b')
22
+
23
+ def __init__(self, enc_key):
24
+ self._enc_key = enc_key
25
+ self._c = 1
26
+ self._b = bytearray()
27
+
28
+
29
+ @classmethod
30
+ def get_cipher(cls, enc_key: bytes):
31
+ return cls(enc_key)
32
+
33
+ def update(self, encrypted_data: bytes) -> bytes:
34
+ print(f"拼接:{len(self._b)} + {len(encrypted_data)} = {len(self._b) + len(encrypted_data)}")
35
+ self._b.extend(encrypted_data)
36
+ buffer = bytearray()
37
+
38
+ for offset in range(0, len(self._b), 16):
39
+ data = self._b[offset:offset + 16]
40
+
41
+ d = Cipher(
42
+ algorithms.AES(self._enc_key),
43
+ modes.CTR(self._c.to_bytes(16, 'little'))
44
+ ).decryptor()
45
+
46
+ if len(data) == 16:
47
+ self._c += 1
48
+ buffer.extend(d.update(data))
49
+
50
+ else:
51
+ self._b = self._b[offset:]
52
+ print("裁剪:", len(self._b))
53
+ break
54
+
55
+ return buffer
56
+
57
+ def finalize(self) -> bytes:
58
+ d = Cipher(
59
+ algorithms.AES(self._enc_key),
60
+ modes.CTR(self._c.to_bytes(16, 'little'))
61
+ ).decryptor()
62
+
63
+ k = d.update(self._b)
64
+ print("K", k)
65
+ return k
66
+
67
+
68
+ def install():
69
+ warnings.warn(
70
+ "The 'cryptography' backend is deprecated and will be removed in version 1.0. "
71
+ "We strongly recommend switching to 'pycryptodome' for better performance. "
72
+ "To migrate, import `rtzip.backends.pycryptodome_impl` and call `install()`.",
73
+ DeprecationWarning,
74
+ stacklevel=2
75
+ )
76
+
77
+ from rtzip.zip_aes import install
78
+ install(pbkdf2_hmac_impl, CryptographyAESCipher)
@@ -5,8 +5,8 @@ from typing import Protocol
5
5
 
6
6
  from obstruct import Struct, u16, u8
7
7
 
8
- from .errors import UnsupportedAlgorithmError, WrongPasswordError
9
8
  from .algorithm_register import algorithm_handler, is_algorithm_handlier_available, get_algorithm_handler
9
+ from .errors import UnsupportedAlgorithmError, WrongPasswordError
10
10
 
11
11
 
12
12
  @dataclasses.dataclass(slots=True)
@@ -175,7 +175,7 @@ async def zip_aes_stream_decryptor(
175
175
  encrypted_data_len = compressed_size - enc_header_len - mac_length
176
176
 
177
177
  # 获取派生密钥
178
- key_material = call_pbkdf2_hmac(pwd, 2 * key_len + 2, salt, 1000)
178
+ key_material = call_pbkdf2_hmac(pwd, 2 * key_len + 2, bytes(salt), 1000)
179
179
  enc_key = key_material[:key_len]
180
180
  hmac_key = key_material[key_len:key_len * 2]
181
181
  checksum = key_material[-2:]
@@ -236,6 +236,10 @@ async def zip_aes_stream_decryptor(
236
236
 
237
237
  # print("Get chunk {}/{}".format(size, encrypted_data_len))
238
238
 
239
+ x = cipher.finalize()
240
+ if x:
241
+ yield x
242
+
239
243
  # 该读取 MAC 了
240
244
 
241
245
  while len(buf) < mac_length:
@@ -1,39 +0,0 @@
1
- # Written by DeepSeek
2
-
3
- from cryptography.hazmat.primitives import hashes
4
- from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
5
- from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
6
-
7
-
8
- def pbkdf2_hmac_impl(pwd: bytes, length: int, salt: bytes, iterations: int) -> bytes:
9
- kdf = PBKDF2HMAC(
10
- algorithm=hashes.SHA1(),
11
- length=length,
12
- salt=salt,
13
- iterations=iterations,
14
- )
15
- return kdf.derive(pwd) # 注意:密码需要单独传递
16
-
17
-
18
- class CryptographyAESCipher:
19
- __slots__ = ('_d', )
20
-
21
- def __init__(self, decryptor):
22
- self._d = decryptor
23
-
24
- @classmethod
25
- def get_cipher(cls, enc_key: bytes):
26
- cipher = Cipher(algorithms.AES(enc_key), modes.CTR((1).to_bytes(16, 'little')))
27
- decryptor = cipher.decryptor()
28
- return cls(decryptor)
29
-
30
- def update(self, encrypted_data: bytes) -> bytes:
31
- return self._d.update(encrypted_data)
32
-
33
- def finalize(self) -> bytes:
34
- return self._d.finalize()
35
-
36
-
37
- def install():
38
- from rtzip.zip_aes import install
39
- install(pbkdf2_hmac_impl, CryptographyAESCipher)
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes