o
    &zh"                  	   @   s   d dl Z d dlmZ d dlmZ d dlmZ d dlmZ dZ	dd Z
d	eeef d
efddZdd Zdd Zdd ZdefddZ	ddededed
efddZdS )    N)md5)Union)generic)rc4_encrypts    (N^NuAd NV.. h>/dSizc           
      C   s   | t  dd } t| }|| td| }|| || |dkr.|s.|d | }|dkrGtdD ]}	t|d|  }q:|d| S )z{
    Implementation of algorithm 3.2 of the PDF standard security handler,
    section 3.5.2 of the PDF 1.6 reference.
    N    z<I   s      2   )_encryption_paddingr   updatestructpack	as_uint32digestrange)
passwordrevkeylenowner_entryp_entry	id1_entryencrypt_metadatammd5_hashi r   Z/var/www/html/kangema/venv/lib/python3.10/site-packages/pyhanko/pdf_utils/crypt/_legacy.pyderive_legacy_file_key   s   



r   r   returnc                 C   s(   t | trt| d d S | d d S )Nr   )
isinstancestrr   encode_pdfdocencoding)r   r   r   r   legacy_normalise_pw@   s   
r"   c                    sb   t | ||}|t dd }t||}|dkr/tddD ] t fdd|D }t||}q|S )z{
    Implementation of algorithm 3.3 of the PDF standard security handler,
    section 3.5.2 of the PDF 1.6 reference.
    Nr   r         c                 3       | ]}| A V  qd S Nr   .0br   r   r   	<genexpr>]       z)compute_o_value_legacy.<locals>.<genexpr>)compute_o_value_legacy_prepr
   r   r   bytes)	owner_pwduser_pwdr   r   keyvalnew_keyr   r*   r   compute_o_value_legacyG   s   
r4   c                 C   sR   | t  dd } t| }| }|dkr!tdD ]}t| }q|d| }|S )z$
    Steps 1-4 of algorithm 3.3
    Nr   r   r	   )r
   r   r   r   )r   r   r   r   r   r   r1   r   r   r   r-   d   s   r-   c              	   C   s(   t | dd|||dd}t|t}||fS ){
    Implementation of algorithm 3.4 of the PDF standard security handler,
    section 3.5.2 of the PDF 1.6 reference.
          Tr   )r   r   r
   )r   r   r   r   r1   ur   r   r   compute_u_value_r2~   s
   
r:   r   c              	      s~   t | ||||||d}t }|t || | }	t||	}
tddD ] t fdd|D }t||
}
q&|
d |fS )r5   r8   r#   r$   c                 3   r%   r&   r   r'   r*   r   r   r+      r,   z&compute_u_value_r34.<locals>.<genexpr>s                   )r   r   r   r
   r   r   r   r.   )r   r   r   r   r   r   r   r1   r   r   r2   r3   r   r*   r   compute_u_value_r34   s$   


r;   F
shared_keyidnum
generationc                 C   s~   t d|dd }t d|dd }| | | }t|t| d ks&J |r,|d7 }t| }|dtdt| d  S )a  
    Function that does the key derivation for PDF's legacy security handlers.

    :param shared_key:
        Global file encryption key.
    :param idnum:
        ID of the object being written.
    :param generation:
        Generation number of the object being written.
    :param use_aes:
        Boolean indicating whether the security handler uses RC4 or AES(-128).
    :return:
    z<iNr   r6   r7   s   sAlT   )r   r   lenr   r   min)r<   r=   r>   use_aespack1pack2r1   r   r   r   r   legacy_derive_object_key   s   rE   )F)r   hashlibr   typingr   pyhanko.pdf_utilsr   pyhanko.pdf_utils.crypt._utilr   r
   r   r    r.   r"   r4   r-   r:   boolr;   intrE   r   r   r   r   <module>   s2    1
9