o
    Xzj  ã                   @   sZ   d Z ddlmZ ddlmZmZ ddlmZmZ ddl	m
Z
 ddlmZ G dd	„ d	ƒZd
S )z3
This module holds the CdnDecrypter utility class.
é    )Úsha256é   )ÚGetCdnFileRequestÚReuploadCdnFileRequest)ÚCdnFileReuploadNeededÚCdnFile)Ú
AESModeCTR)ÚCdnFileTamperedErrorc                   @   s8   e Zd ZdZdd„ Zedd„ ƒZdd„ Zedd	„ ƒZd
S )ÚCdnDecrypterzÕ
    Used when downloading a file results in a 'FileCdnRedirect' to
    both prepare the redirect, decrypt the file as it downloads, and
    ensure the file hasn't been tampered. https://core.telegram.org/cdn
    c                 C   s   || _ || _|| _|| _dS )a.  
        Initializes the CDN decrypter.

        :param cdn_client: a client connected to a CDN.
        :param file_token: the token of the file to be used.
        :param cdn_aes: the AES CTR used to decrypt the file.
        :param cdn_file_hashes: the hashes the decrypted file must match.
        N)ÚclientÚ
file_tokenÚcdn_aesÚcdn_file_hashes)ÚselfÚ
cdn_clientr   r   r   © r   úW/var/www/kodo/tsheker/venv/lib/python3.10/site-packages/telethon/crypto/cdndecrypter.pyÚ__init__   s   	
zCdnDecrypter.__init__c                 Ã   sÄ   �t |j|jdd… tdƒ d�}t||j||jƒ}|t|j|jd j|jd j	d�ƒI dH }t
|tƒrI| t|j|jd�ƒI dH  | ¡ }||fS |j |j¡|_|j d¡}| |j|¡ ||fS )a9  
        Prepares a new CDN decrypter.

        :param client: a TelegramClient connected to the main servers.
        :param cdn_client: a new client connected to the CDN.
        :param cdn_redirect: the redirect file object that caused this call.
        :return: (CdnDecrypter, first chunk file data)
        Né   é   )ÚkeyÚivr   )r   ÚoffsetÚlimit)r   Úrequest_token)r   Úencryption_keyÚencryption_ivÚbytesr
   r   r   r   r   r   Ú
isinstancer   r   r   Úget_filer   ÚencryptÚpopÚcheck)r   r   Úcdn_redirectr   Ú	decrypterÚcdn_fileÚcdn_hashr   r   r   Úprepare_decrypter    s2   €
ýþ

ý
þüzCdnDecrypter.prepare_decrypterc                 C   s\   | j r&| j  d¡}|  t| j|j|jƒ¡}| j |j	¡|_	|  
|j	|¡ |S tt	dƒƒ}|S )z 
        Calls GetCdnFileRequest and decrypts its bytes.
        Also ensures that the file hasn't been tampered.

        :return: the CdnFile result.
        r   )r   r!   r   r   r   r   r   r   r    r   r"   r   )r   r&   r%   r   r   r   r   L   s   ÿþzCdnDecrypter.get_filec                 C   s   t | ƒ ¡ |jkrtƒ ‚dS )zÔ
        Checks the integrity of the given data.
        Raises CdnFileTamperedError if the integrity check fails.

        :param data: the data to be hashed.
        :param cdn_hash: the expected hash.
        N)r   ÚdigestÚhashr	   )Údatar&   r   r   r   r"   _   s   	ÿzCdnDecrypter.checkN)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Ústaticmethodr'   r   r"   r   r   r   r   r
      s    
+r
   N)r.   Úhashlibr   Útl.functions.uploadr   r   Útl.types.uploadr   r   Úcryptor   Úerrorsr	   r
   r   r   r   r   Ú<module>   s    