From 4c9d9d84f289c40c3c9b44491db47d2c88b00204 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Sat, 1 Sep 2018 01:27:22 +0200 Subject: [PATCH] Change the way int to bytes conversion is used Maybe at some point I should switch to struct --- pyrogram/api/core/primitives/bool.py | 2 +- pyrogram/api/core/primitives/bytes.py | 2 +- pyrogram/api/core/primitives/int.py | 2 +- pyrogram/api/core/primitives/null.py | 2 +- pyrogram/crypto/rsa.py | 14 +++++--------- pyrogram/session/auth.py | 20 ++++++++++---------- 6 files changed, 19 insertions(+), 23 deletions(-) diff --git a/pyrogram/api/core/primitives/bool.py b/pyrogram/api/core/primitives/bool.py index cc8655d9..9641e865 100644 --- a/pyrogram/api/core/primitives/bool.py +++ b/pyrogram/api/core/primitives/bool.py @@ -30,7 +30,7 @@ class BoolFalse(Object): return cls.value def __new__(cls) -> bytes: - return int.to_bytes(cls.ID, 4, "little") + return cls.ID.to_bytes(4, "little") class BoolTrue(BoolFalse): diff --git a/pyrogram/api/core/primitives/bytes.py b/pyrogram/api/core/primitives/bytes.py index da438ef1..d161cc9c 100644 --- a/pyrogram/api/core/primitives/bytes.py +++ b/pyrogram/api/core/primitives/bytes.py @@ -48,7 +48,7 @@ class Bytes(Object): else: return ( bytes([254]) - + int.to_bytes(length, 3, "little") + + length.to_bytes(3, "little") + value + bytes(-length % 4) ) diff --git a/pyrogram/api/core/primitives/int.py b/pyrogram/api/core/primitives/int.py index 0985367f..3de37151 100644 --- a/pyrogram/api/core/primitives/int.py +++ b/pyrogram/api/core/primitives/int.py @@ -29,7 +29,7 @@ class Int(Object): return int.from_bytes(b.read(cls.SIZE), "little", signed=signed) def __new__(cls, value: int, signed: bool = True) -> bytes: - return int.to_bytes(value, cls.SIZE, "little", signed=signed) + return value.to_bytes(cls.SIZE, "little", signed=signed) class Long(Int): diff --git a/pyrogram/api/core/primitives/null.py b/pyrogram/api/core/primitives/null.py index 3d73c06e..7a26b112 100644 --- a/pyrogram/api/core/primitives/null.py +++ b/pyrogram/api/core/primitives/null.py @@ -29,4 +29,4 @@ class Null(Object): return None def __new__(cls) -> bytes: - return int.to_bytes(cls.ID, 4, "little") + return cls.ID.to_bytes(4, "little") diff --git a/pyrogram/crypto/rsa.py b/pyrogram/crypto/rsa.py index 9f02e2cc..10302dab 100644 --- a/pyrogram/crypto/rsa.py +++ b/pyrogram/crypto/rsa.py @@ -206,12 +206,8 @@ class RSA: @classmethod def encrypt(cls, data: bytes, fingerprint: int) -> bytes: - return int.to_bytes( - pow( - int.from_bytes(data, "big"), - cls.server_public_keys[fingerprint].e, - cls.server_public_keys[fingerprint].m - ), - 256, - "big" - ) + return pow( + int.from_bytes(data, "big"), + cls.server_public_keys[fingerprint].e, + cls.server_public_keys[fingerprint].m + ).to_bytes(256, "big") diff --git a/pyrogram/session/auth.py b/pyrogram/session/auth.py index a545a642..87817da1 100644 --- a/pyrogram/session/auth.py +++ b/pyrogram/session/auth.py @@ -111,8 +111,8 @@ class Auth: data = types.PQInnerData( res_pq.pq, - int.to_bytes(p, 4, "big"), - int.to_bytes(q, 4, "big"), + p.to_bytes(4, "big"), + q.to_bytes(4, "big"), nonce, server_nonce, new_nonce, @@ -131,8 +131,8 @@ class Auth: functions.ReqDHParams( nonce, server_nonce, - int.to_bytes(p, 4, "big"), - int.to_bytes(q, 4, "big"), + p.to_bytes(4, "big"), + q.to_bytes(4, "big"), public_key_fingerprint, encrypted_data ) @@ -140,8 +140,8 @@ class Auth: encrypted_answer = server_dh_params.encrypted_answer - server_nonce = int.to_bytes(server_nonce, 16, "little", signed=True) - new_nonce = int.to_bytes(new_nonce, 32, "little", signed=True) + server_nonce = server_nonce.to_bytes(16, "little", signed=True) + new_nonce = new_nonce.to_bytes(32, "little", signed=True) tmp_aes_key = ( sha1(new_nonce + server_nonce).digest() @@ -170,7 +170,7 @@ class Auth: # Step 6 g = server_dh_inner_data.g b = int.from_bytes(urandom(256), "big") - g_b = int.to_bytes(pow(g, b, dh_prime), 256, "big") + g_b = pow(g, b, dh_prime).to_bytes(256, "big") retry_id = 0 @@ -199,8 +199,8 @@ class Auth: # Step 7; Step 8 g_a = int.from_bytes(server_dh_inner_data.g_a, "big") - auth_key = int.to_bytes(pow(g_a, b, dh_prime), 256, "big") - server_nonce = int.to_bytes(server_nonce, 16, "little", signed=True) + auth_key = pow(g_a, b, dh_prime).to_bytes(256, "big") + server_nonce = server_nonce.to_bytes(16, "little", signed=True) # TODO: Handle errors @@ -235,7 +235,7 @@ class Auth: # 3rd message assert nonce == set_client_dh_params_answer.nonce assert server_nonce == set_client_dh_params_answer.server_nonce - server_nonce = int.to_bytes(server_nonce, 16, "little", signed=True) + server_nonce = server_nonce.to_bytes(16, "little", signed=True) log.debug("Nonce fields check: OK") # Step 9