rtzip 0.4.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.4.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.4.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.4.0"
9
+ version = "0.5.1"
10
10
  requires-python = ">=3.12"
11
11
  dependencies = [
12
12
  "obstruct==0.5.2",
@@ -0,0 +1,75 @@
1
+ from typing import AsyncGenerator, Any, Protocol, runtime_checkable
2
+
3
+ from .errors import UnsupportedAlgorithmError
4
+ from .models import CDEntry, LocalFileHeader
5
+
6
+ type IndexAcceptedTypes = bytes | bytearray | memoryview
7
+
8
+
9
+ @runtime_checkable
10
+ class AlgorithmHandler(Protocol):
11
+ def __call__(
12
+ self,
13
+ raw_data: AsyncGenerator[IndexAcceptedTypes, None],
14
+ entry: CDEntry,
15
+ header: LocalFileHeader,
16
+ context: dict[str, Any],
17
+ /
18
+ ) -> AsyncGenerator[IndexAcceptedTypes, None]:
19
+ """
20
+ 压缩算法处理器
21
+
22
+ :param raw_data: 原始数据流(异步生成器)
23
+ :param entry: 当前文件的 CDEntry 对象
24
+ :param header: 当前文件的 LocalFileHeader 对象(已经过文件描述符修补)
25
+ :param context: 上下文信息
26
+ :return:
27
+ """
28
+ ...
29
+
30
+
31
+ algorithm_mapping: dict[int, AlgorithmHandler] = {}
32
+
33
+
34
+ def algorithm_handler(algorithm: int, override=False):
35
+ """
36
+ 压缩流包装器注册装饰器
37
+
38
+ :param algorithm: 压缩算法 ID
39
+ :param override: 如果你希望覆盖一个已注册的包装器,请设置为 True,否则将会阻止可能的覆盖行为
40
+ :return:
41
+ """
42
+ if not override and algorithm in algorithm_mapping:
43
+ raise ValueError(f"Algorithm {algorithm} is already registered")
44
+
45
+ def decorator(func: AlgorithmHandler):
46
+ algorithm_mapping[algorithm] = func
47
+ return func
48
+
49
+ return decorator
50
+
51
+
52
+ def get_algorithm_handler(algorithm: int) -> AlgorithmHandler:
53
+ """
54
+ 获取包装器函数
55
+
56
+ :param algorithm: 压缩算法 ID
57
+ :return: AlgorithmHandler 可调用对象,接受一个异步生成器,返回一个异步生成器
58
+ """
59
+ try:
60
+ return algorithm_mapping[algorithm]
61
+ except KeyError:
62
+ raise UnsupportedAlgorithmError(algorithm)
63
+
64
+
65
+ def get_algorithm_handler_safe(algorithm: int) -> AlgorithmHandler | None:
66
+ return algorithm_mapping.get(algorithm)
67
+
68
+ def is_algorithm_handlier_available(algorithm: int) -> bool:
69
+ """
70
+ 判断某个压缩算法是否已有注册的包装器
71
+
72
+ :param algorithm: 压缩算法 ID
73
+ :return:
74
+ """
75
+ return algorithm in algorithm_mapping
@@ -1,9 +1,9 @@
1
1
  import struct
2
2
  from collections.abc import Buffer
3
3
  from fnmatch import fnmatch
4
- from typing import AsyncGenerator
4
+ from typing import AsyncGenerator, Any
5
5
 
6
- from .algorithm_register import is_algorithm_available, get_algorithm_handler
6
+ from .algorithm_register import is_algorithm_handlier_available, get_algorithm_handler
7
7
  from .const import *
8
8
  from .errors import UnsupportedAlgorithmError
9
9
  from .models import EOCD, Zip64EOCD, CDEntry, LocalFileHeader
@@ -66,11 +66,11 @@ class RemoteZip:
66
66
  return await self.source.get_total_size()
67
67
 
68
68
  __slots__ = [
69
- 'source', 'zip_info', 'files', 'file_mapping', 'cached_headers', 'is_zip64',
69
+ 'source', 'zip_info', 'files', 'file_mapping', 'cached_headers', 'is_zip64', 'context',
70
70
  '_namelist'
71
71
  ]
72
72
 
73
- def __init__(self, source: DataSource):
73
+ def __init__(self, source: DataSource, **kwargs):
74
74
  self.source = source
75
75
 
76
76
  self.zip_info: EOCD | Zip64EOCD | None = None
@@ -80,8 +80,15 @@ class RemoteZip:
80
80
  self.cached_headers: dict[bytes, LocalFileHeader] = {}
81
81
 
82
82
  self.is_zip64 = False
83
+ self.context = kwargs
83
84
  self._namelist = None
84
85
 
86
+ def _merge_context(self, overrides: dict[str, Any]):
87
+ return {
88
+ **self.context,
89
+ **overrides
90
+ }
91
+
85
92
  def namelist(self):
86
93
  if self._namelist is None:
87
94
  if self.files is None:
@@ -264,7 +271,7 @@ class RemoteZip:
264
271
  entry = entry or self.find_file_entry(normpath(bytes(filename)))
265
272
 
266
273
  if reuse and entry.filename in self.cached_headers:
267
- return self.cached_headers[entry.filename]
274
+ return self.cached_headers[entry.filename], b''
268
275
 
269
276
  buffer = await self._read_range(entry.local_header_offset, entry.__cstruct__.size + 64) # 预留64字节的变长字段
270
277
 
@@ -342,9 +349,11 @@ class RemoteZip:
342
349
  raise FileNotFoundError(filename)
343
350
 
344
351
  async def _stream_single_file(self, filename: bytes, kwargs):
352
+ kwargs = self._merge_context(kwargs)
353
+
345
354
  entry = self.find_file_entry(filename)
346
355
 
347
- if not is_algorithm_available(entry.algorithm):
356
+ if not is_algorithm_handlier_available(entry.algorithm):
348
357
  raise UnsupportedAlgorithmError(entry.algorithm)
349
358
 
350
359
  header, buffer = await self.fetch_file_header(entry=entry)
@@ -396,6 +405,7 @@ class RemoteZip:
396
405
 
397
406
 
398
407
  async def resolve(self, filename: bytes, max_resolve=24, **kwargs) -> bytes:
408
+ kwargs = self._merge_context(kwargs)
399
409
  entry = self.find_file_entry(filename)
400
410
  target = entry.filename
401
411
 
@@ -440,8 +450,8 @@ class RemoteZip:
440
450
  filename=normpath(path)
441
451
  )
442
452
 
443
- async def read_text(self, path: bytes, encoding='utf-8', errors='strict') -> str:
444
- return (await self.read(path)).decode(encoding, errors=errors)
453
+ async def read_text(self, path: bytes, encoding='utf-8', errors='strict', **kwargs) -> str:
454
+ return (await self.read(path, **kwargs)).decode(encoding, errors=errors)
445
455
 
446
456
  def rglob(self, pattern):
447
457
  for file in self._namelist:
@@ -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 .algorithm_register import algorithm_handler, is_algorithm_handlier_available, get_algorithm_handler
8
9
  from .errors import UnsupportedAlgorithmError, WrongPasswordError
9
- from .algorithm_register import algorithm_handler, is_algorithm_available, get_algorithm_handler
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:
@@ -278,7 +282,7 @@ def zip_aes_wrapper(raw_data, entry, header, ctx):
278
282
  # 获取解密后的压缩格式
279
283
  algorithm = aes_extra.compression
280
284
 
281
- if not is_algorithm_available(algorithm):
285
+ if not is_algorithm_handlier_available(algorithm):
282
286
  raise UnsupportedAlgorithmError(algorithm)
283
287
 
284
288
  return get_algorithm_handler(algorithm)(
@@ -290,4 +294,4 @@ def zip_aes_wrapper(raw_data, entry, header, ctx):
290
294
  def install(pbkdf2_hmac: PBKDF2_HMAC_FunctionType, aes_cipher: type[AESDecryptor]):
291
295
  pbkdf2_hmac_implement(pbkdf2_hmac)
292
296
  aes_cipher_implement(aes_cipher)
293
- algorithm_handler(0x0063)(zip_aes_wrapper) if not is_algorithm_available(0x0063) else None
297
+ algorithm_handler(0x0063)(zip_aes_wrapper) if not is_algorithm_handlier_available(0x0063) else None
@@ -1,46 +0,0 @@
1
- from collections.abc import Buffer
2
- from typing import Callable, AsyncGenerator, Any
3
-
4
- from .models import CDEntry, LocalFileHeader
5
-
6
- AlgorithmHandler = Callable[[AsyncGenerator[Buffer, None], CDEntry, LocalFileHeader, dict[str, Any]], AsyncGenerator[bytes | bytearray | memoryview, None]]
7
-
8
- algorithm_mapping: dict[int, AlgorithmHandler] = {}
9
-
10
-
11
- def algorithm_handler(algorithm: int, override=False):
12
- """
13
- 压缩流包装器注册装饰器
14
-
15
- :param algorithm: 压缩算法 ID
16
- :param override: 如果你希望覆盖一个已注册的包装器,请设置为 True,否则将会阻止可能的覆盖行为
17
- :return:
18
- """
19
- if not override and algorithm in algorithm_mapping:
20
- raise ValueError(f"Algorithm {algorithm} is already registered")
21
-
22
- def decorator(func: AlgorithmHandler):
23
- algorithm_mapping[algorithm] = func
24
- return func
25
-
26
- return decorator
27
-
28
-
29
- def get_algorithm_handler(algorithm: int) -> AlgorithmHandler:
30
- """
31
- 获取包装器函数
32
-
33
- :param algorithm: 压缩算法 ID
34
- :return: AlgorithmHandler 可调用对象,接受一个异步生成器,返回一个异步生成器
35
- """
36
- return algorithm_mapping[algorithm]
37
-
38
-
39
- def is_algorithm_available(algorithm: int) -> bool:
40
- """
41
- 判断某个压缩算法是否已有注册的包装器
42
-
43
- :param algorithm: 压缩算法 ID
44
- :return:
45
- """
46
- return algorithm in algorithm_mapping
@@ -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