U
    Óøïc¨1  ã                
   @   s6  d Z ddlZddlmZ ddlmZmZ ddlmZm	Z	m
Z
 ddlmZ zddlmZ W n  ek
rx   ddlmZ Y nX d	Zdeed eeeeeedœdd„Zeeed eedœdd„Zeed eedœdd„Zeeeeeeef dœdd„Zeed eeeeeeeef dœdd„Zeeef eedœdd„ZdS )z,Anything related to encryption / decryption.é    N)Úmd5)ÚTupleÚUnioné   )Úb_Úord_Ústr_)ÚByteStringObject)ÚLiterals    (¿N^NuŠAd NVÿú.. ¶Ðh>€/©þdSizT)é   é   é   )ÚpasswordÚrevÚkeylenÚowner_entryÚp_entryÚ	id1_entryÚmetadata_encryptÚreturnc                 C   s¦   t t| ƒttƒ dd… ƒ}t|ƒ}| |j¡ t d|¡}	| |	¡ | |j¡ |dkrh|sh| d¡ | ¡ }
|dkršt	dƒD ]}t|
d|… ƒ ¡ }
q€|
d|… S )a  
    Implementation of algorithm 3.2 of the PDF standard security handler.

    See section 3.5.2 of the PDF 1.6 reference.

    Args:
        password: The encryption secret as a bytes-string
        rev: The encryption revision (see PDF standard)
        keylen:
        owner_entry:
        p_entry: A set of flags specifying which operations shall be permitted
            when the document is opened with user access. If bit 2 is set to 1, all other
            bits are ignored and all operations are permitted. If bit 2 is set to 0,
            permission for operations are based on the values of the remaining flags
            defined in Table 24.
        id1_entry:
        metadata_encrypt:  (Default value = True)

    Returns:
        An MD5 hash of keylen characters.
    Né    z<ir   s   ÿÿÿÿé2   )
r   r   Ú_encryption_paddingr   ÚupdateÚoriginal_bytesÚstructÚpackÚdigestÚrange)r   r   r   r   r   r   r   Úpassword_bytesÚmZp_entry_bytesÚmd5_hashÚ_© r#   ú3/tmp/pip-unpacked-wheel-u_n6aelj/pypdf/_security.pyÚ_alg326   s    #

r%   )Úowner_passwordÚuser_passwordr   r   r   c                    sl   t | ||ƒ}t|ttƒ dd… ƒ}t||ƒ}|dkrhtddƒD ]&‰ d ‡ fdd„|D ƒ¡}t||ƒ}q@|S )	aL  
    Implementation of algorithm 3.3 of the PDF standard security handler,
    section 3.5.2 of the PDF 1.6 reference.

    Args:
        owner_password:
        user_password:
        rev: The encryption revision (see PDF standard)
        keylen:

    Returns:
        A transformed version of the owner and the user password
    Nr   r   r   é   Ú c                 3   s   | ]}t t|ƒˆ A ƒV  qd S )N)Úchrr   )Ú.0Zkey_char©Úir#   r$   Ú	<genexpr>œ   s     z_alg33.<locals>.<genexpr>)Ú_alg33_1r   r   r   ÚRC4_encryptr   Újoin)r&   r'   r   r   ÚkeyZuser_password_bytesÚvalÚnew_keyr#   r,   r$   Ú_alg33|   s    
r5   )r   r   r   r   c                 C   sZ   t | ttƒ dd… ƒ}t|ƒ}| ¡ }|dkrJtdƒD ]}t|ƒ ¡ }q8|d|… }|S )zÆ
    Steps 1-4 of algorithm 3.3.

    Args:
        password:
        rev: The encryption revision (see PDF standard)
        keylen:

    Returns:
        A transformed version of the password
    Nr   r   r   )r   r   r   r   r   r   )r   r   r   r   r    r!   r"   r2   r#   r#   r$   r/   £   s    r/   )r   r   r   r   r   c                 C   s,   d}d}t | |||||ƒ}t|tƒ}||fS )af  
    Implementation of algorithm 3.4 of the PDF standard security handler.

    See section 3.5.2 of the PDF 1.6 reference.

    Args:
        password:
        owner_entry:
        p_entry: A set of flags specifying which operations shall be permitted
            when the document is opened with user access. If bit 2 is set to 1, all other
            bits are ignored and all operations are permitted. If bit 2 is set to 0,
            permission for operations are based on the values of the remaining flags
            defined in Table 24.
        id1_entry:

    Returns:
        A Tuple (u-value, key)
    r   é   )r%   r0   r   )r   r   r   r   r   r   r2   ÚUr#   r#   r$   Ú_alg34Ä   s
    
r8   c                 C   sŒ   t | |||||ƒ}tƒ }| t¡ | |j¡ | ¡ }	t||	ƒ}
tddƒD ]4}d}|D ]}|tt	t
|ƒ|A ƒƒ7 }qVt||
ƒ}
qJ|
d |fS )aÐ  
    Implementation of algorithm 3.4 of the PDF standard security handler.

    See section 3.5.2 of the PDF 1.6 reference.

    Args:
        password:
        rev: The encryption revision (see PDF standard)
        keylen:
        owner_entry:
        p_entry: A set of flags specifying which operations shall be permitted
            when the document is opened with user access. If bit 2 is set to 1, all other
            bits are ignored and all operations are permitted. If bit 2 is set to 0,
            permission for operations are based on the values of the remaining flags
            defined in Table 24.
        id1_entry:
        metadata_encrypt: A boolean

    Returns:
        A tuple (value, key)
    r   r(   ó    s                   )r%   r   r   r   r   r   r0   r   r   r*   r   )r   r   r   r   r   r   r   r2   r    r!   r3   r-   r4   Úkr#   r#   r$   Ú_alg35ê   s     

r;   )r2   Ú	plaintextr   c                 C   sä   t tdƒƒ}d}tdƒD ]B}|||  t| |t| ƒ  ƒ d }|| ||  ||< ||< qd\}}g }|D ]l}|d d }|||  d }|| ||  ||< ||< ||| ||  d  }| ttt|ƒ|A ƒƒ¡ qld |¡S )Né   r   )r   r   r   r9   )Úlistr   r   ÚlenÚappendr   r*   r1   )r2   r<   ÚSÚjr-   ÚretvalZplaintext_charÚtr#   r#   r$   r0   ,  s    $r0   )T)Ú__doc__r   Úhashlibr   Útypingr   r   Ú_utilsr   r   r   Zgenericr	   r
   ÚImportErrorZtyping_extensionsr   ÚstrÚintÚboolÚbytesr%   r5   r/   r8   r;   r0   r#   r#   r#   r$   Ú<module>   sZ   ÿ ùøG   þ'"
û'
øB