2017-06-02 17:49:03 +03:00
|
|
|
from hashlib import sha1, sha256
|
2016-11-30 00:29:42 +03:00
|
|
|
import os
|
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)
|
|
|
|
|
2016-09-08 17:11:37 +03:00
|
|
|
# endregion
|
|
|
|
|
|
|
|
# region Cryptographic related utils
|
2016-08-30 18:40:49 +03:00
|
|
|
|
|
|
|
|
2016-08-26 13:58:53 +03:00
|
|
|
def calc_key(shared_key, msg_key, client):
|
2016-08-28 14:43:00 +03:00
|
|
|
"""Calculate the key based on Telegram guidelines, specifying whether it's the client or not"""
|
2016-08-26 13:58:53 +03:00
|
|
|
x = 0 if client else 8
|
|
|
|
|
2017-06-02 17:49:03 +03:00
|
|
|
sha1a = sha1(msg_key + shared_key[x:x + 32]).digest()
|
|
|
|
sha1b = sha1(shared_key[x + 32:x + 48] + msg_key +
|
|
|
|
shared_key[x + 48:x + 64]).digest()
|
|
|
|
|
|
|
|
sha1c = sha1(shared_key[x + 64:x + 96] + msg_key).digest()
|
|
|
|
sha1d = sha1(msg_key + shared_key[x + 96:x + 128]).digest()
|
2016-08-26 13:58:53 +03:00
|
|
|
|
|
|
|
key = sha1a[0:8] + sha1b[8:20] + sha1c[4:16]
|
|
|
|
iv = sha1a[8:20] + sha1b[0:8] + sha1c[16:20] + sha1d[0:8]
|
|
|
|
|
|
|
|
return key, iv
|
|
|
|
|
|
|
|
|
|
|
|
def calc_msg_key(data):
|
2016-08-28 14:43:00 +03:00
|
|
|
"""Calculates the message key from the given data"""
|
2017-06-02 17:49:03 +03:00
|
|
|
return sha1(data).digest()[4:20]
|
2016-08-26 13:58:53 +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-06-02 17:49:03 +03:00
|
|
|
hash1 = sha1(bytes(new_nonce + server_nonce)).digest()
|
|
|
|
hash2 = sha1(bytes(server_nonce + new_nonce)).digest()
|
|
|
|
hash3 = sha1(bytes(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-11-26 14:04:02 +03:00
|
|
|
def get_password_hash(pw, current_salt):
|
|
|
|
"""Gets the password hash for the two-step verification.
|
2017-05-21 14:59:16 +03:00
|
|
|
current_salt should be the byte array provided by invoking GetPasswordRequest()"""
|
2016-11-26 14:04:02 +03:00
|
|
|
|
|
|
|
# Passwords are encoded as UTF-8
|
2017-05-21 14:59:16 +03:00
|
|
|
# At https://github.com/DrKLO/Telegram/blob/e31388
|
|
|
|
# src/main/java/org/telegram/ui/LoginActivity.java#L2003
|
2016-11-26 14:04:02 +03:00
|
|
|
data = pw.encode('utf-8')
|
|
|
|
|
2016-11-30 00:29:42 +03:00
|
|
|
pw_hash = current_salt + data + current_salt
|
2017-06-02 17:49:03 +03:00
|
|
|
return sha256(pw_hash).digest()
|
2016-11-26 14:04:02 +03:00
|
|
|
|
2016-09-08 17:11:37 +03:00
|
|
|
# endregion
|