3
¬Vèe•  ã               @   s4  d Z ddlmZ ddlZddlZddlZddlmZ ddlm	Z	 ddl
mZ d$Zd
Zejd dkZejd dkZer’dd„ ZeZdd„ Zdd„ Zn(erºeZdd„ Zdd„ Zdd„ Zdd„ ZeedƒrÎdd„ Zndd„ Zde_ G dd„ deƒZG dd „ d eƒZG d!d„ deƒZd"d„ Zejj e_ d#d	„ Zejj e_ dS )%z#Python implementation of RNCryptor.é    )Úprint_functionN)ÚRandom)ÚAES)ÚKDFÚ	RNCryptorÚdecryptÚencryptz3.3.0é   é   c             C   s&   t | tƒr| S t | tƒr"| jdƒS d S )Nzutf-8)Ú
isinstanceÚstrÚunicodeÚencode)Ús© r   ú./tmp/pip-build-72n0iy2u/rncryptor/rncryptor.pyÚto_bytes   s    

r   c             C   s   t | ƒS )N)Úchr)r   r   r   r   Úbchr   s    r   c             C   s   t | ƒS )N)Úord)r   r   r   r   Úbord   s    r   c             C   s&   t | tƒr| S t | tƒr"| jdƒS d S )Nzutf-8)r   Úbytesr   r   )r   r   r   r   r   $   s    

c             C   s&   t | tƒr| jdƒS t | tƒr"| S d S )Nzutf-8)r   r   Údecoder   )r   r   r   r   Úto_str*   s    


r   c             C   s
   t | gƒS )N)r   )r   r   r   r   r   0   s    c             C   s   | S )Nr   )r   r   r   r   r   3   s    Úcompare_digestc             C   s   t j| |ƒS )N)Úhmacr   )ÚleftÚrightr   r   r   Úcompare_in_constant_time8   s    r   c             C   sR   t | ƒ}t |ƒ}|| }x0t|ƒD ]$\}}|t| ||  ƒt|ƒA O }q"W |dkS )Nr   )ÚlenÚ	enumerater   )r   r   Zlength_leftZlength_rightÚresultÚiZbyter   r   r   r   ;   s     zrCompare two values in time proportional to the second one.

Return True if the values are equal, False otherwise.
c               @   s   e Zd ZdZdS )ÚRNCryptorErrorz7Base error for when anything goes wrong with RNCryptor.N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r#   L   s   r#   c               @   s   e Zd ZdZdS )ÚDecryptionErrorz!Raised when bad data is inputted.N)r$   r%   r&   r'   r   r   r   r   r(   P   s   r(   c               @   s’   e Zd ZdZdZdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Z	dd„ Z
edd„ ƒZedd„ ƒZedd„ ƒZdd„ Zdd„ Zdd„ Zdd„ Zd"dd „Zd!S )#r   zCryptor for RNCryptor.é   c             C   s   t |ƒ}|S )zHandle data before decryption.)r   )ÚselfÚdatar   r   r   Úpre_decrypt_dataY   s    zRNCryptor.pre_decrypt_datac             C   s   |dt |d ƒ … }t|ƒS )zBRemove useless symbols which appear over padding for AES (PKCS#7).Né   éÿÿÿÿ)r   r   )r*   r+   r   r   r   Úpost_decrypt_data^   s    zRNCryptor.post_decrypt_datac             C   s´   | j |ƒ}t|ƒ}t|ƒ}|dd… }|dd… }|dd… }|d|d … }||d d… }| j||ƒ}	| j||ƒ}
t| j|
|d|d … ƒ|ƒsœtdƒ‚| j|	||ƒ}| j|ƒS )z Decrypt `data` using `password`.r	   é
   é   é"   é    NzBad data)	r,   r   r   Ú_pbkdf2r   Ú_hmacr(   Ú_aes_decryptr/   )r*   r+   ÚpasswordÚnÚencryption_saltÚ	hmac_saltÚivÚcipher_textr   Úencryption_keyÚhmac_keyZdecrypted_datar   r   r   r   c   s    
zRNCryptor.decryptc             C   s.   t |ƒ}tj}|t|ƒ|  }|t|ƒ|  S )z)Do padding for the data for AES (PKCS#7).)r   r   Ú
block_sizer   r   )r*   r+   Zaes_block_sizeÚremr   r   r   Úpre_encrypt_data|   s    zRNCryptor.pre_encrypt_datac             C   s   |S )zHandle data after encryption.r   )r*   r+   r   r   r   Úpost_encrypt_dataƒ   s    zRNCryptor.post_encrypt_datac             C   s‚   | j |ƒ}t|ƒ}| j}| j||ƒ}| j}| j||ƒ}| j}| j|||ƒ}d}	d}
dj|	|
||||gƒ}|| j||ƒ }| j	|ƒS )z Encrypt `data` using `password`.ó   ó   ó    )
rA   r   r9   r4   r:   r;   Ú_aes_encryptÚjoinr5   rB   )r*   r+   r7   r9   r=   r:   r>   r;   r<   ÚversionÚoptionsZnew_dataZencrypted_datar   r   r   r   ‡   s    
zRNCryptor.encryptc             C   s   t jƒ j| jƒS )N)r   ÚnewÚreadÚ	SALT_SIZE)r*   r   r   r   r9   �   s    zRNCryptor.encryption_saltc             C   s   t jƒ j| jƒS )N)r   rJ   rK   rL   )r*   r   r   r   r:   ¡   s    zRNCryptor.hmac_saltc             C   s   t jƒ jtjƒS )N)r   rJ   rK   r   r?   )r*   r   r   r   r;   ¥   s    zRNCryptor.ivc             C   s   t j|t j|ƒj|ƒS )N)r   rJ   ÚMODE_CBCr   )r*   Úkeyr;   Útextr   r   r   rF   ©   s    zRNCryptor._aes_encryptc             C   s   t j|t j|ƒj|ƒS )N)r   rJ   rM   r   )r*   rN   r;   rO   r   r   r   r6   ¬   s    zRNCryptor._aes_decryptc             C   s   t j||tjƒjƒ S )N)r   rJ   ÚhashlibÚsha256Údigest)r*   rN   r+   r   r   r   r5   ¯   s    zRNCryptor._hmacc             C   s   t j||tjƒjƒ S )N)r   rJ   rP   Úsha1rR   )r*   ZsecretÚsaltr   r   r   Ú_prf²   s    zRNCryptor._prfé'  r3   c             C   s   t j||||| jd�S )N)ZdkLenÚcountZprf)r   ZPBKDF2rU   )r*   r7   rT   Z
iterationsZ
key_lengthr   r   r   r4   µ   s    zRNCryptor._pbkdf2N)rV   r3   )r$   r%   r&   r'   rL   r,   r/   r   rA   rB   r   Úpropertyr9   r:   r;   rF   r6   r5   rU   r4   r   r   r   r   r   T   s    c             C   s   t ƒ }|j| |ƒS )N)r   r   )r+   r7   Úcryptorr   r   r   r   ¹   s    c             C   s   t ƒ }|j| |ƒS )N)r   r   )r+   r7   rY   r   r   r   r   Á   s    )r   r   r   ) r'   Ú
__future__r   rP   r   ÚsysZCryptor   ZCrypto.Cipherr   ZCrypto.Protocolr   Ú__all__Ú__version__Úversion_infoÚPY2ÚPY3r   r   r   r   r   r   Úhasattrr   Ú	Exceptionr#   r(   Úobjectr   r   r   r   r   r   r   Ú<module>   sB   


e
