3
uQhB  ã               @   s–   d Z ddlZddlZddlZddlmZ ejeƒZyddl	Z	ej
dƒ W n: ek
r‚   dZ	ejrtejrtej
dƒ n
ej
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 j| ||ƒS tjr&tj| ||ƒS |dt|ƒd … }|t|ƒd d… }tj|ƒ}g }t| ƒd }dgd }x”t|ƒD ]ˆ}	x,tdƒD ] }
| |	d |
  ||
 A ||
< qŠW |j|ƒ}x$tdƒD ]}
||
  ||
 N  < qÂW | |	d |	d d … }|}|j|ƒ q|W t	|ƒ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-build-2nz6shyl/telethon/telethon/crypto/aes.pyr   "   s(    

 
zAES.decrypt_igec             C   s<  t | ƒd }|r"| tjd| ƒ7 } tr4tj| ||ƒS tjrHtj| ||ƒS |dt |ƒd … }|t |ƒd d… }tj|ƒ}g }t | ƒd }x¨t|ƒD ]œ}	t	| |	d |	d d … ƒ}
x$tdƒD ]}|
|  || N  < q¾W |j
|
ƒ}x$tdƒD ]}||  || N  < qîW |}| |	d |	d d … }|j|ƒ q”W 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   s0    

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   
