Added and updated documentation

This commit is contained in:
Lonami
2016-08-28 13:43:00 +02:00
parent 5af1a4a5fc
commit bd1fee4048
16 changed files with 177 additions and 139 deletions

View File

@@ -4,6 +4,7 @@ from hashlib import sha1
def generate_random_long(signed=True):
"""Generates a random long integer (8 bytes), which is optionally signed"""
result = random.getrandbits(64)
if not signed:
result &= 0xFFFFFFFFFFFFFFFF # Ensure it's unsigned
@@ -12,6 +13,7 @@ def generate_random_long(signed=True):
def generate_random_bytes(count):
"""Generates a random bytes array"""
with BinaryWriter() as writer:
for _ in range(count):
writer.write(random.getrandbits(8))
@@ -20,6 +22,7 @@ def generate_random_bytes(count):
def calc_key(shared_key, msg_key, client):
"""Calculate the key based on Telegram guidelines, specifying whether it's the client or not"""
x = 0 if client else 8
buffer = [0] * 48
@@ -47,10 +50,12 @@ def calc_key(shared_key, msg_key, client):
def calc_msg_key(data):
"""Calculates the message key from the given data"""
return sha1(data)[4:20]
def calc_msg_key_offset(data, offset, limit):
"""Calculates the message key from offset given data, with an optional offset and limit"""
# TODO untested, may not be offset like this
# In the original code it was as parameters for the sha function, not slicing the array
return sha1(data[offset:offset + limit])[4:20]