o
    >eI                     @   sv  d dl Z d dlZd dlmZ d dlmZ d dlmZmZ d dl	m
Z
 d dlmZ d dlmZ d dlmZ d d	lmZ d
dgZejdddejdedejdejddddejdddedejddddejdddddd Zejdd
ddd
 Zejdd deddd!Zejd"d#dejdedejdejddddejdd$deded%d& Zejd'd(dejdedejdejddddejd)edd*d+ejd,d-edd.ejd/ded0d1d2d3ejd4d5edddd6ed7d8 Z d9ej!fd:d;Z"ejd<d=dejdedejdejddddejd>edejd/ded?d@d2d3edAdB Z#dS )C    N)cli_root)pyhanko_exception_manager)_warn_empty_passphrasereadable_file)load_certs_from_pemder)crypt)StandardSecurityHandler)PdfFileReader)copy_into_new_writerdecryptencrypt_filez encrypt PDF files (AES-256 only)encrypt)helpnameinfile)typeoutfileTF)writabledir_okayz
--passwordz!password to encrypt the file with)r   requiredr   z--recipientzIcertificate(s) corresponding to entities that can decrypt the output file)readabler   )r   multipler   r   c           	   
   C   s  |r	|r	t d|s|stjdd}d }|rtt|d}t V t| d8}t|}t|}|r8|	| n|j
|d t|d}|| W d    n1 sSw   Y  W d    n1 sbw   Y  W d    d S W d    d S 1 szw   Y  d S )Nz2Specify either a password or a list of recipients.zOutput file password: prompt)
cert_filesrb)
owner_passwb)clickClickExceptiongetpasslistr   r   openr	   r
   encrypt_pubkeyr   write)	r   r   password	recipientrecipient_certsinfrwoutf r,   [/var/www/html/humari/django-venv/lib/python3.10/site-packages/pyhanko/cli/commands/crypt.pyr      s0   "z6decrypt PDF files (any standard PDF encryption scheme)c                   C   s   d S )Nr,   r,   r,   r,   r-   r   <   s   z--forcez1ignore access restrictions (use at your own risk))r   r   r   is_flagdefaultzdecrypt using passwordr%   z!password to decrypt the file withc           	   
   C   s"  t   t| df}t|}|jd u rtdt|jts#td|s+tjdd}|	|}|j
tjjkr>|s>td|j
tjjkrJtdt|}t|d}|| W d    n1 scw   Y  W d    n1 srw   Y  W d    d S W d    d S 1 sw   Y  d S )	Nr   File is not encrypted.zIFile is not encrypted with the standard (password-based) security handlerzFile password: r   zjPassword specified was the user password, not the owner password. Pass --force to decrypt the file anyway.zPassword didn't match.r   )r   r"   r	   security_handlerr   r   
isinstancer   r    r   statusr   
AuthStatusUSERFAILEDr
   r$   )	r   r   r%   forcer(   r)   auth_resultr*   r+   r,   r,   r-   decrypt_with_passwordN   s6   



"r9   z#decrypt using private key (PEM/DER)pemderz--keyz5file containing the recipient's private key (PEM/DER))r   r   r   z--certz5file containing the recipient's certificate (PEM/DER))r   r   r   z
--passfiler   z2file containing the passphrase for the private keystdin)r   r   r   show_defaultz	--no-passz*assume the private key file is unencrypted)r   r   r.   r/   r<   c           	      C   sh   |d ur|  }|  n|s tjddd}|st  d }nd }tjj|||d}t|| || d S )NKey passphrase: r   utf-8)key_passphrase)	readcloser    encoder   r   SimpleEnvelopeKeyDecrypterload_decrypt_pubkey)	r   r   keycertpassfiler7   no_pass
passphrasesedkr,   r,   r-   decrypt_with_pemderr   s    
rL   rK   c           	   
   C   s  t  ~ t|d`}t|}|jd u rtdt|jtjs$td|	| }|j
tjjkr8|s7tdn|j
tjjkrDtdt|}t|d}|| W d    n1 s]w   Y  W d    n1 slw   Y  W d    d S W d    d S 1 sw   Y  d S )Nr   r0   z:File was not encrypted with a public-key security handler.zhChange of encryption is typically not allowed with user access. Pass --force to decrypt the file anyway.zFailed to decrypt the file.r   )r   r"   r	   r1   r   r   r2   r   PubKeySecurityHandlerdecrypt_pubkeyr3   r4   r5   r6   r
   r$   )	rK   r   r   r7   r(   r)   r8   r*   r+   r,   r,   r-   rE      s6   



"rE   z#decrypt using private key (PKCS#12)pkcs12pfxr)   z3file containing the passphrase for the PKCS#12 filec                 C   sX   |d u rt j ddd}n|  d}|  tjj||d}t|| || d S )Nr=   r   r>   )rJ   )	r    rB   readlinestriprA   r   rC   load_pkcs12rE   )r   r   rP   rH   r7   rJ   rK   r,   r,   r-   decrypt_with_pkcs12   s   rT   )$r    r   pyhanko.cli._rootr   pyhanko.cli.runtimer   pyhanko.cli.utilsr   r   pyhanko.keysr   pyhanko.pdf_utilsr   pyhanko.pdf_utils.cryptr   pyhanko.pdf_utils.readerr	   pyhanko.pdf_utils.writerr
   __all__commandargumentPathoptionstrr   groupr   booldecrypt_force_flagr9   FilerL   rC   rE   rT   r,   r,   r,   r-   <module>   s    


