From 6c67693a3e24fc27b8fc2ef20526068ed7b44605 Mon Sep 17 00:00:00 2001 From: Michal Charemza Date: Wed, 3 Jan 2024 16:07:07 +0000 Subject: [PATCH] feat: AES-2 encryption --- pyproject.toml | 4 ++ stream_zip.py | 91 +++++++++++++++++++++++++++------------------- test_stream_zip.py | 44 ++++++++++++++++++++++ 3 files changed, 102 insertions(+), 37 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 1cf5107..42d6b6b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -16,6 +16,9 @@ classifiers = [ "License :: OSI Approved :: MIT License", "Topic :: System :: Archiving :: Compression", ] +dependencies = [ + "pycryptodome>=3.10.1", +] [project.optional-dependencies] dev = [ @@ -25,6 +28,7 @@ dev = [ "stream-unzip>=0.0.86" ] ci = [ + "pycryptodome==3.10.1", "coverage==6.2", "pytest==6.2.5", "pytest-cov==3.0.0", diff --git a/stream_zip.py b/stream_zip.py index b2c6439..fbe933a 100644 --- a/stream_zip.py +++ b/stream_zip.py @@ -1,7 +1,13 @@ from collections import deque from struct import Struct +import secrets import zlib +from Crypto.Cipher import AES +from Crypto.Hash import HMAC, SHA1 +from Crypto.Util import Counter +from Crypto.Protocol.KDF import PBKDF2 + # Private methods _NO_COMPRESSION_BUFFERED_32 = object() @@ -63,7 +69,7 @@ def method_compressobj(offset, default_get_compressobj): return method_compressobj -def stream_zip(files, chunk_size=65536, get_compressobj=lambda: zlib.compressobj(wbits=-zlib.MAX_WBITS, level=9), extended_timestamps=True): +def stream_zip(files, chunk_size=65536, get_compressobj=lambda: zlib.compressobj(wbits=-zlib.MAX_WBITS, level=9), extended_timestamps=True, password=None): def evenly_sized(chunks): chunk = b'' @@ -119,6 +125,9 @@ def get_zipped_chunks_uneven(): mod_at_unix_extra_signature = b'UT' mod_at_unix_extra_struct = Struct('<2sH1sl') + aes_extra_signature = b'\x01\x99' + aes_extra_struct = Struct('<2sHH2sBH') + modified_at_struct = Struct('