2017-06-09 17:13:39 +03:00
|
|
|
"""Various helpers not related to the Telegram API itself"""
|
2016-11-30 00:29:42 +03:00
|
|
|
import os
|
2018-06-29 12:04:42 +03:00
|
|
|
import struct
|
2018-01-06 03:55:11 +03:00
|
|
|
from hashlib import sha1, sha256
|
|
|
|
|
2016-08-30 18:40:49 +03:00
|
|
|
|
2016-09-08 17:11:37 +03:00
|
|
|
# region Multiple utilities
|
2016-08-26 13:58:53 +03:00
|
|
|
|
|
|
|
|
|
|
|
def generate_random_long(signed=True):
|
2016-08-28 14:43:00 +03:00
|
|
|
"""Generates a random long integer (8 bytes), which is optionally signed"""
|
2016-09-03 11:54:58 +03:00
|
|
|
return int.from_bytes(os.urandom(8), signed=signed, byteorder='little')
|
2016-08-26 13:58:53 +03:00
|
|
|
|
|
|
|
|
2016-09-12 20:32:16 +03:00
|
|
|
def ensure_parent_dir_exists(file_path):
|
|
|
|
"""Ensures that the parent directory exists"""
|
|
|
|
parent = os.path.dirname(file_path)
|
|
|
|
if parent:
|
|
|
|
os.makedirs(parent, exist_ok=True)
|
|
|
|
|
2018-06-29 12:04:42 +03:00
|
|
|
|
|
|
|
def add_surrogate(text):
|
|
|
|
return ''.join(
|
|
|
|
# SMP -> Surrogate Pairs (Telegram offsets are calculated with these).
|
|
|
|
# See https://en.wikipedia.org/wiki/Plane_(Unicode)#Overview for more.
|
|
|
|
''.join(chr(y) for y in struct.unpack('<HH', x.encode('utf-16le')))
|
|
|
|
if (0x10000 <= ord(x) <= 0x10FFFF) else x for x in text
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def del_surrogate(text):
|
|
|
|
return text.encode('utf-16', 'surrogatepass').decode('utf-16')
|
|
|
|
|
|
|
|
|
2018-11-19 12:15:56 +03:00
|
|
|
def strip_text(text, entities):
|
|
|
|
"""
|
|
|
|
Strips whitespace from the given text modifying the provided entities.
|
|
|
|
|
|
|
|
This assumes that there are no overlapping entities, that their length
|
|
|
|
is greater or equal to one, and that their length is not out of bounds.
|
|
|
|
"""
|
|
|
|
if not entities:
|
|
|
|
return text.strip()
|
|
|
|
|
|
|
|
while text and text[-1].isspace():
|
|
|
|
e = entities[-1]
|
|
|
|
if e.offset + e.length == len(text):
|
|
|
|
if e.length == 1:
|
|
|
|
del entities[-1]
|
|
|
|
if not entities:
|
|
|
|
return text.strip()
|
|
|
|
else:
|
|
|
|
e.length -= 1
|
|
|
|
text = text[:-1]
|
|
|
|
|
|
|
|
while text and text[0].isspace():
|
2018-12-06 14:33:15 +03:00
|
|
|
for i in reversed(range(len(entities))):
|
|
|
|
e = entities[i]
|
|
|
|
if e.offset != 0:
|
|
|
|
e.offset -= 1
|
|
|
|
continue
|
|
|
|
|
2018-11-19 12:15:56 +03:00
|
|
|
if e.length == 1:
|
|
|
|
del entities[0]
|
|
|
|
if not entities:
|
|
|
|
return text.lstrip()
|
|
|
|
else:
|
|
|
|
e.length -= 1
|
2018-12-06 14:33:15 +03:00
|
|
|
|
2018-11-19 12:15:56 +03:00
|
|
|
text = text[1:]
|
|
|
|
|
|
|
|
return text
|
|
|
|
|
|
|
|
|
2016-09-08 17:11:37 +03:00
|
|
|
# endregion
|
|
|
|
|
|
|
|
# region Cryptographic related utils
|
2016-08-30 18:40:49 +03:00
|
|
|
|
|
|
|
|
2017-05-21 14:59:16 +03:00
|
|
|
def generate_key_data_from_nonce(server_nonce, new_nonce):
|
|
|
|
"""Generates the key data corresponding to the given nonce"""
|
2017-09-28 12:36:51 +03:00
|
|
|
server_nonce = server_nonce.to_bytes(16, 'little', signed=True)
|
|
|
|
new_nonce = new_nonce.to_bytes(32, 'little', signed=True)
|
|
|
|
hash1 = sha1(new_nonce + server_nonce).digest()
|
|
|
|
hash2 = sha1(server_nonce + new_nonce).digest()
|
|
|
|
hash3 = sha1(new_nonce + new_nonce).digest()
|
2016-08-30 18:40:49 +03:00
|
|
|
|
2016-09-17 21:42:34 +03:00
|
|
|
key = hash1 + hash2[:12]
|
|
|
|
iv = hash2[12:20] + hash3 + new_nonce[:4]
|
|
|
|
return key, iv
|
2016-08-30 18:40:49 +03:00
|
|
|
|
|
|
|
|
2016-09-08 17:11:37 +03:00
|
|
|
# endregion
|
2018-08-03 00:00:10 +03:00
|
|
|
|
|
|
|
# region Custom Classes
|
|
|
|
|
2018-10-19 14:24:52 +03:00
|
|
|
|
2018-08-03 00:00:10 +03:00
|
|
|
class TotalList(list):
|
|
|
|
"""
|
|
|
|
A list with an extra `total` property, which may not match its `len`
|
|
|
|
since the total represents the total amount of items *available*
|
|
|
|
somewhere else, not the items *in this list*.
|
|
|
|
"""
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self.total = 0
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return '[{}, total={}]'.format(
|
|
|
|
', '.join(str(x) for x in self), self.total)
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return '[{}, total={}]'.format(
|
|
|
|
', '.join(repr(x) for x in self), self.total)
|
|
|
|
|
2018-09-29 14:29:44 +03:00
|
|
|
|
2018-08-03 00:00:10 +03:00
|
|
|
# endregion
|