Skip to content

Commit 00b4645

Browse files
committed
refactor: chunker
1 parent a2ef458 commit 00b4645

File tree

2 files changed

+14
-15
lines changed

2 files changed

+14
-15
lines changed

src/mega/chunker.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -84,16 +84,16 @@ def _iter_chunks(
8484
"""AES-128-CCM (CTR + CBC-MAC) implementation"""
8585
# TODO: pycrypto probably has its own implementation
8686
key_bytes = a32_to_bytes(key)
87-
nonce = ((iv[0] << 32) + iv[1]) << 64
88-
aes = AES.new(key_bytes, AES.MODE_CTR, counter=Counter.new(128, initial_value=nonce))
89-
9087
mac_bytes = EMPTY_IV
91-
mac_cypher = AES.new(key_bytes, AES.MODE_CBC, mac_bytes)
9288
iv_bytes = a32_to_bytes([iv[0], iv[1], iv[0], iv[1]])
93-
data_in: bytes | None = yield b""
89+
nonce = ((iv[0] << 32) + iv[1]) << 64
9490

91+
aes = AES.new(key_bytes, AES.MODE_CTR, counter=Counter.new(128, initial_value=nonce))
92+
mac_cypher = AES.new(key_bytes, AES.MODE_CBC, EMPTY_IV)
9593
chunk_cypher = AES.new(key_bytes, AES.MODE_CBC, iv_bytes)
9694

95+
data_in: bytes | None = yield b""
96+
9797
while data_in is not None:
9898
if decrypt:
9999
decrypted_data = data_out = aes.decrypt(data_in)
@@ -102,10 +102,10 @@ def _iter_chunks(
102102
data_out = aes.encrypt(data_in)
103103

104104
mem_view = memoryview(decrypted_data)
105-
last_16b_index = (len(decrypted_data) % AES.block_size) or AES.block_size
106-
last_16b = pad_bytes(mem_view[-last_16b_index:])
107-
chunk_cypher.encrypt(mem_view[:-last_16b_index])
108-
mac_bytes = mac_cypher.encrypt(chunk_cypher.encrypt(last_16b))
105+
last_block_index = (len(decrypted_data) % AES.block_size) or AES.block_size
106+
last_block = pad_bytes(mem_view[-last_block_index:])
107+
chunk_cypher.encrypt(mem_view[:-last_block_index])
108+
mac_bytes = mac_cypher.encrypt(chunk_cypher.encrypt(last_block))
109109
data_in = yield data_out
110110

111111
file_mac = str_to_a32(mac_bytes)

src/mega/crypto.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,12 @@ def a32_to_bytes(array: Sequence[int]) -> bytes:
106106
return struct.pack(f">{len(array):.0f}I", *array)
107107

108108

109-
def str_to_a32(bytes_or_str: str | bytes) -> tuple[int, ...]:
110-
if isinstance(bytes_or_str, str):
111-
bytes_ = bytes_or_str.encode()
109+
def str_to_a32(data: str | bytes, /) -> tuple[int, ...]:
110+
if isinstance(data, str):
111+
bytes_ = data.encode()
112112
else:
113-
assert isinstance(bytes_or_str, bytes)
114-
bytes_ = bytes_or_str
113+
assert isinstance(data, bytes)
114+
bytes_ = data
115115

116116
bytes_ = pad_bytes(bytes_, length=4)
117117
return struct.unpack(f">{len(bytes_) // 4}I", bytes_)
@@ -126,7 +126,6 @@ def mpi_to_int(data: bytes) -> int:
126126

127127

128128
def b64_url_decode(data: str) -> bytes:
129-
# def d64(data_str: str) -> bytes:
130129
return base64.urlsafe_b64decode(data + "=" * (-len(data) % 4))
131130

132131

0 commit comments

Comments
 (0)