From c667a00281d82c560bc25637fa57d67cacfd742d Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Wed, 27 Sep 2017 21:23:59 +0200 Subject: [PATCH] Avoid using BinaryWriter where possible --- telethon/crypto/auth_key.py | 12 ++++-------- telethon/crypto/rsa.py | 11 +++++------ telethon/network/connection.py | 35 ++++++++++++++-------------------- telethon/tl/gzip_packed.py | 8 +++----- 4 files changed, 26 insertions(+), 40 deletions(-) diff --git a/telethon/crypto/auth_key.py b/telethon/crypto/auth_key.py index 02774d58..710ebed8 100644 --- a/telethon/crypto/auth_key.py +++ b/telethon/crypto/auth_key.py @@ -1,7 +1,8 @@ +import struct from hashlib import sha1 from .. import helpers as utils -from ..extensions import BinaryReader, BinaryWriter +from ..extensions import BinaryReader class AuthKey: @@ -17,10 +18,5 @@ class AuthKey: """Calculates the new nonce hash based on the current class fields' values """ - with BinaryWriter() as writer: - writer.write(new_nonce) - writer.write_byte(number) - writer.write_long(self.aux_hash, signed=False) - - new_nonce_hash = utils.calc_msg_key(writer.get_bytes()) - return new_nonce_hash + data = new_nonce + struct.pack('> 2 - if length < 127: - writer.write_byte(length) - else: - writer.write_byte(127) - writer.write(int.to_bytes(length, 3, 'little')) - writer.write(message) - self.write(writer.get_bytes()) + length = len(message) >> 2 + if length < 127: + length = struct.pack('B', length) + else: + length = b'\x7f' + int.to_bytes(length, 3, 'little') + + self.write(length + message) # endregion diff --git a/telethon/tl/gzip_packed.py b/telethon/tl/gzip_packed.py index c2fe17c7..9a0d547a 100644 --- a/telethon/tl/gzip_packed.py +++ b/telethon/tl/gzip_packed.py @@ -1,7 +1,7 @@ import gzip +import struct from . import TLObject -from ..extensions import BinaryWriter class GzipPacked(TLObject): @@ -29,10 +29,8 @@ class GzipPacked(TLObject): def to_bytes(self): # TODO Maybe compress level could be an option - with BinaryWriter() as writer: - writer.write_int(GzipPacked.constructor_id, signed=False) - writer.tgwrite_bytes(gzip.compress(self.data)) - return writer.get_bytes() + return struct.pack('