U
    ¯ªhB  ã                   @   s–   d Z ddlZddlZddlZddlmZ e e¡Zzddl	Z	e 
d¡ W n: ek
r‚   dZ	ejrtejrte 
d¡ n
e 
d¡ Y nX G dd	„ d	ƒZdS )
z¶
AES IGE implementation in Python.

If available, cryptg will be used instead, otherwise
if available, libssl will be used instead, otherwise
the Python implementation will be used.
é    Né   )Úlibsslz/cryptg detected, it will be used for encryptionz/libssl detected, it will be used for encryptionz\cryptg module not installed and libssl not found, falling back to (slower) Python encryptionc                   @   s(   e Zd ZdZedd„ ƒZedd„ ƒZdS )ÚAESzf
    Class that servers as an interface to encrypt and decrypt
    text through the AES IGE mode.
    c                 C   s  t rt  | ||¡S tjr&t | ||¡S |dt|ƒd … }|t|ƒd d… }t |¡}g }t| ƒd }dgd }t|ƒD ]€}	tdƒD ] }
| |	d |
  ||
 A ||
< q†| |¡}tdƒD ]}
||
  ||
 N  < qº| |	d |	d d … }|}| |¡ qzt	|ƒS )z
        Decrypts the given text in 16-bytes blocks by using the
        given key and 32-bytes initialization vector.
        Né   é   r   )
ÚcryptgÚdecrypt_iger   ÚlenÚpyaesr   ÚrangeZdecryptÚextendÚbytes)Úcipher_textÚkeyÚivÚiv1Úiv2ÚaesÚ
plain_textÚblocks_countÚcipher_text_blockÚblock_indexÚiÚplain_text_block© r   ú7/tmp/pip-unpacked-wheel-c81u5j2r/telethon/crypto/aes.pyr   "   s*    

ÿ
zAES.decrypt_igec                 C   s0  t | ƒd }|r"| t d| ¡7 } tr4t | ||¡S tjrHt | ||¡S |dt |ƒd … }|t |ƒd d… }t |¡}g }t | ƒd }t|ƒD ]”}	t	| |	d |	d d … ƒ}
tdƒD ]}|
|  || N  < qº| 
|
¡}tdƒD ]}||  || N  < qæ|}| |	d |	d d … }| |¡ q’t|ƒS )z
        Encrypts the given text in 16-bytes blocks by using the
        given key and 32-bytes initialization vector.
        r   Nr   )r	   ÚosÚurandomr   Úencrypt_iger   r
   r   r   ÚlistZencryptr   r   )r   r   r   Úpaddingr   r   r   r   r   r   r   r   r   r   r   r   r   G   s2    
ÿ
zAES.encrypt_igeN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ústaticmethodr   r   r   r   r   r   r      s
   
$r   )r$   r   r
   ÚloggingÚ r   Ú	getLoggerr!   Z__log__r   ÚinfoÚImportErrorr   r   r   r   r   r   r   Ú<module>   s   
