Telethon/telethon/crypto/authkey.py

41 lines
1.2 KiB
Python
Raw Normal View History

2017-11-26 18:57:40 +03:00
"""
This module holds the AuthKey class.
"""
import struct
from hashlib import sha1
from ..extensions import BinaryReader
2016-08-28 20:26:06 +03:00
class AuthKey:
2017-11-26 18:57:40 +03:00
"""
Represents an authorization key, used to encrypt and decrypt
messages sent to Telegram's data centers.
"""
def __init__(self, data):
2017-11-26 18:57:40 +03:00
"""
Initializes a new authorization key.
:param data: the data in bytes that represent this auth key.
"""
self.key = data
2016-08-28 20:26:06 +03:00
with BinaryReader(sha1(self.key).digest()) as reader:
2016-08-28 20:26:06 +03:00
self.aux_hash = reader.read_long(signed=False)
reader.read(4)
self.key_id = reader.read_long(signed=False)
def calc_new_nonce_hash(self, new_nonce, number):
2017-11-26 18:57:40 +03:00
"""
Calculates the new nonce hash based on the current attributes.
:param new_nonce: the new nonce to be hashed.
:param number: number to prepend before the hash.
:return: the hash for the given new nonce.
2017-09-04 18:10:04 +03:00
"""
new_nonce = new_nonce.to_bytes(32, 'little', signed=True)
data = new_nonce + struct.pack('<BQ', number, self.aux_hash)
# Calculates the message key from the given data
return int.from_bytes(sha1(data).digest()[4:20], 'little', signed=True)