From 6e4ee71c32098c518246ae276aa531f0404c5168 Mon Sep 17 00:00:00 2001 From: Jahongir Qurbonov Date: Fri, 12 Sep 2025 16:33:53 +0500 Subject: [PATCH] Refactor AuthKey class documentation and improve key_id extraction logic --- client/src/telethon/_impl/crypto/auth_key.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/client/src/telethon/_impl/crypto/auth_key.py b/client/src/telethon/_impl/crypto/auth_key.py index 90afc306..efdc0155 100644 --- a/client/src/telethon/_impl/crypto/auth_key.py +++ b/client/src/telethon/_impl/crypto/auth_key.py @@ -6,15 +6,26 @@ from typing_extensions import Self @dataclass class AuthKey: + """Represents a Telegram's authorization key. + + To generate a new, valid authorization key, one should use the methods + provided by the generation module. + + Authorization key: https://core.telegram.org/mtproto/auth_key + """ + data: bytes aux_hash: bytes key_id: bytes @classmethod def from_bytes(cls, data: bytes) -> Self: + if len(data) != 256: + raise ValueError("Auth key data must be exactly 256 bytes") + sha = sha1(data).digest() aux_hash = sha[:8] - key_id = sha[12:] + key_id = sha[12:20] return cls(data=data, aux_hash=aux_hash, key_id=key_id) def __bytes__(self) -> bytes: