Skip to content

Commit 4c9d9d8

Browse files
committed
Change the way int to bytes conversion is used
Maybe at some point I should switch to struct
1 parent 07a9cce commit 4c9d9d8

File tree

6 files changed

+19
-23
lines changed

6 files changed

+19
-23
lines changed

pyrogram/api/core/primitives/bool.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def read(cls, *args) -> bool:
3030
return cls.value
3131

3232
def __new__(cls) -> bytes:
33-
return int.to_bytes(cls.ID, 4, "little")
33+
return cls.ID.to_bytes(4, "little")
3434

3535

3636
class BoolTrue(BoolFalse):

pyrogram/api/core/primitives/bytes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def __new__(cls, value: bytes) -> bytes:
4848
else:
4949
return (
5050
bytes([254])
51-
+ int.to_bytes(length, 3, "little")
51+
+ length.to_bytes(3, "little")
5252
+ value
5353
+ bytes(-length % 4)
5454
)

pyrogram/api/core/primitives/int.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def read(cls, b: BytesIO, signed: bool = True) -> int:
2929
return int.from_bytes(b.read(cls.SIZE), "little", signed=signed)
3030

3131
def __new__(cls, value: int, signed: bool = True) -> bytes:
32-
return int.to_bytes(value, cls.SIZE, "little", signed=signed)
32+
return value.to_bytes(cls.SIZE, "little", signed=signed)
3333

3434

3535
class Long(Int):

pyrogram/api/core/primitives/null.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,4 @@ def read(b: BytesIO, *args) -> None:
2929
return None
3030

3131
def __new__(cls) -> bytes:
32-
return int.to_bytes(cls.ID, 4, "little")
32+
return cls.ID.to_bytes(4, "little")

pyrogram/crypto/rsa.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -206,12 +206,8 @@ class RSA:
206206

207207
@classmethod
208208
def encrypt(cls, data: bytes, fingerprint: int) -> bytes:
209-
return int.to_bytes(
210-
pow(
211-
int.from_bytes(data, "big"),
212-
cls.server_public_keys[fingerprint].e,
213-
cls.server_public_keys[fingerprint].m
214-
),
215-
256,
216-
"big"
217-
)
209+
return pow(
210+
int.from_bytes(data, "big"),
211+
cls.server_public_keys[fingerprint].e,
212+
cls.server_public_keys[fingerprint].m
213+
).to_bytes(256, "big")

pyrogram/session/auth.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,8 @@ def create(self):
111111

112112
data = types.PQInnerData(
113113
res_pq.pq,
114-
int.to_bytes(p, 4, "big"),
115-
int.to_bytes(q, 4, "big"),
114+
p.to_bytes(4, "big"),
115+
q.to_bytes(4, "big"),
116116
nonce,
117117
server_nonce,
118118
new_nonce,
@@ -131,17 +131,17 @@ def create(self):
131131
functions.ReqDHParams(
132132
nonce,
133133
server_nonce,
134-
int.to_bytes(p, 4, "big"),
135-
int.to_bytes(q, 4, "big"),
134+
p.to_bytes(4, "big"),
135+
q.to_bytes(4, "big"),
136136
public_key_fingerprint,
137137
encrypted_data
138138
)
139139
)
140140

141141
encrypted_answer = server_dh_params.encrypted_answer
142142

143-
server_nonce = int.to_bytes(server_nonce, 16, "little", signed=True)
144-
new_nonce = int.to_bytes(new_nonce, 32, "little", signed=True)
143+
server_nonce = server_nonce.to_bytes(16, "little", signed=True)
144+
new_nonce = new_nonce.to_bytes(32, "little", signed=True)
145145

146146
tmp_aes_key = (
147147
sha1(new_nonce + server_nonce).digest()
@@ -170,7 +170,7 @@ def create(self):
170170
# Step 6
171171
g = server_dh_inner_data.g
172172
b = int.from_bytes(urandom(256), "big")
173-
g_b = int.to_bytes(pow(g, b, dh_prime), 256, "big")
173+
g_b = pow(g, b, dh_prime).to_bytes(256, "big")
174174

175175
retry_id = 0
176176

@@ -199,8 +199,8 @@ def create(self):
199199

200200
# Step 7; Step 8
201201
g_a = int.from_bytes(server_dh_inner_data.g_a, "big")
202-
auth_key = int.to_bytes(pow(g_a, b, dh_prime), 256, "big")
203-
server_nonce = int.to_bytes(server_nonce, 16, "little", signed=True)
202+
auth_key = pow(g_a, b, dh_prime).to_bytes(256, "big")
203+
server_nonce = server_nonce.to_bytes(16, "little", signed=True)
204204

205205
# TODO: Handle errors
206206

@@ -235,7 +235,7 @@ def create(self):
235235
# 3rd message
236236
assert nonce == set_client_dh_params_answer.nonce
237237
assert server_nonce == set_client_dh_params_answer.server_nonce
238-
server_nonce = int.to_bytes(server_nonce, 16, "little", signed=True)
238+
server_nonce = server_nonce.to_bytes(16, "little", signed=True)
239239
log.debug("Nonce fields check: OK")
240240

241241
# Step 9

0 commit comments

Comments
 (0)