Telethon/crypto/auth_key.py

31 lines
1.1 KiB
Python
Raw Normal View History

2016-08-28 20:26:06 +03:00
# This file is based on TLSharp
# https://github.com/sochix/TLSharp/blob/master/TLSharp.Core/MTProto/Crypto/AuthKey.cs
2016-09-04 13:42:11 +03:00
import utils
from errors import *
2016-09-04 13:42:11 +03:00
from utils import BinaryWriter, BinaryReader
2016-08-28 20:26:06 +03:00
class AuthKey:
def __init__(self, gab=None, data=None):
if gab:
2016-08-30 18:40:49 +03:00
self.key = utils.get_byte_array(gab, signed=False)
2016-08-28 20:26:06 +03:00
elif data:
self.key = data
else:
raise InvalidParameterError('Either a gab integer or data bytes array must be provided')
2016-08-28 20:26:06 +03:00
2016-08-30 18:40:49 +03:00
with BinaryReader(utils.sha1(self.key)) 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):
"""Calculates the new nonce hash based on the current class fields' values"""
2016-08-28 20:26:06 +03:00
with BinaryWriter() as writer:
writer.write(new_nonce)
writer.write_byte(number)
writer.write_long(self.aux_hash, signed=False)
2016-08-30 18:40:49 +03:00
new_nonce_hash = utils.sha1(writer.get_bytes())[4:20]
2016-08-28 20:26:06 +03:00
return new_nonce_hash