Reuse the AESModeCTR class on CdnDecrypter

This commit is contained in:
Lonami Exo 2017-08-28 20:30:33 +02:00
parent 5404670469
commit bc72e52834
2 changed files with 7 additions and 19 deletions

View File

@ -12,18 +12,10 @@ class AESModeCTR:
assert isinstance(iv, bytes)
assert len(iv) == 16
self.iv = iv
self._aes._counter._counter = list(self.iv)
def reset(self):
pass
self._aes._counter._counter = list(iv)
def encrypt(self, data):
result = self._aes.encrypt(data)
self.reset()
return result
return self._aes.encrypt(data)
def decrypt(self, data):
result = self._aes.decrypt(data)
self.reset()
return result
return self._aes.decrypt(data)

View File

@ -1,9 +1,9 @@
from hashlib import sha256
import pyaes
from ..tl import JsonSession
from ..tl.functions.upload import GetCdnFileRequest, ReuploadCdnFileRequest
from ..tl.types.upload import CdnFileReuploadNeeded
from ..crypto import AESModeCTR
from ..errors import CdnFileTamperedError
@ -29,13 +29,9 @@ class CdnDecrypter:
"""
# TODO Avoid the need for 'client_cls=TelegramBareClient'
# https://core.telegram.org/cdn
# TODO Use libssl if available
cdn_aes = pyaes.AESModeOfOperationCTR(cdn_redirect.encryption_key)
# The returned IV is the counter used on CTR
cdn_aes._counter._counter = list(
cdn_redirect.encryption_iv[:12] +
(offset >> 4).to_bytes(4, 'big')
cdn_aes = AESModeCTR(
key=cdn_redirect.encryption_key,
iv=cdn_redirect.encryption_iv[:12] + (offset >> 4).to_bytes(4, 'big')
)
# Create a new client on said CDN