
    ;Si                     `    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	      Zy
)z3
This module holds the CdnDecrypter utility class.
    )sha256   )GetCdnFileRequestReuploadCdnFileRequest)CdnFileReuploadNeededCdnFile)
AESModeCTR)CdnFileTamperedErrorc                   <    e Zd ZdZd Zed        Zd Zed        Zy)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                 <    || _         || _        || _        || _        y)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   s        a/var/www/OnlineNewsSite/project/venv/lib/python3.12/site-packages/telethon/crypto/cdndecrypter.py__init__zCdnDecrypter.__init__   s!     !$.    c                   K   t        |j                  |j                  dd t        d      z         }t	        ||j
                  ||j                        } |t        |j
                  |j                  d   j                  |j                  d   j                               d{   }t        |t              rC | t        |j
                  |j                               d{    |j                         }||fS |j                  j!                  |j                        |_        |j                  j#                  d      }|j%                  |j                  |       ||fS 7 7 w)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_hashs          r   prepare_decrypterzCdnDecrypter.prepare_decrypter    sR     ++))#2.q9
 !//\11
	
 $$5#..//299..q177%
  
 h 56/'22&44    !))+H (""	 '..66x~~FHN 0044Q7HOOHNNH5(""'s%   B"E%$E!%>E%#E#$A>E%#E%c                    | j                   r| j                   j                  d      }| j                  t        | j                  |j
                  |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)   s      r   r#   zCdnDecrypter.get_fileL   s     ++//2H{{#4(..$ H "\\11(..AHNJJx~~x0  uQx(Hr   c                 d    t        |       j                         |j                  k7  r
t               y)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*   s     r   r&   zCdnDecrypter.check_   s+     $< HMM1&(( 2r   N)	__name__
__module____qualname____doc__r   staticmethodr+   r#   r&    r   r   r   r      s:    
/ )# )#V& 	) 	)r   r   N)r4   hashlibr   tl.functions.uploadr   r   tl.types.uploadr   r   cryptor	   errorsr
   r   r6   r   r   <module>r<      s'     K <  )]) ])r   