o
    6d'                     @   s   d Z ddlZddlZddlZddlZddlZddlZddlZeej	j
 Z
ejejeje f ZdddZG dd dejdZG d	d
 d
eZG dd deZG dd deZG dd deZe Ze Ze Ze ZdS )zWCommandline scripts.

These scripts are called by the executables defined in setup.py.
    Nreturnc            	   
   C   s  t jddd} | jdddd | jdd	dd
d | jddddd | tjdd \}}t|dkr;|   tdzt	|d }W n  t
yc } z|   td|d  tjd td|d}~ww td| tjd t|\}}|jrtd|j tjd |j|jd}t|jd}|| W d   n1 sw   Y  |j|jd}|jrtd|j tjd t|jd}|| W d   dS 1 sw   Y  dS tdtjd tjj| dS )zKey generator.zusage: %prog [options] keysizez/Generates a new RSA key pair of "keysize" bits.usagedescriptionz--puboutstringzOutput filename for the public key. The public key is not saved if this option is not present. You can use pyrsa-priv2pub to create the public key file later.typehelp-oz--outz`Output filename for the private key. The key is written to stdout if this option is not present.z--formz7key format of the private and public keys - default PEMPEMZDERr   r	   choicesdefault   Nr   zNot a valid number: %sfilezGenerating %i-bit keyzWriting public key to %s)formatwbzWriting private key to %szWriting private key to stdout)optparseOptionParser
add_option
parse_argssysargvlen
print_help
SystemExitint
ValueErrorprintstderrrsaZnewkeysZpuboutZ
save_pkcs1formopenwriteoutstdoutbuffer)	parserclicli_argsZkeysizeexpub_keypriv_keydataoutfile r1   ^C:\Users\jesus\OneDrive\Desktop\erpjis_fastapi\backend\jisbackend\Lib\site-packages\rsa/cli.pykeygen!   sb   
"r3   c                	   @   s   e Zd ZdZdZdZdZdZdZdZ	dZ
d	Zd
ZdZejZd$ddZejdedejjdedejfddZd$ddZdejejeje  f fddZ!de de dejjfddZ"de defddZ#d ed!e ddfd"d#Z$dS )%CryptoOperationz9CLI callable that operates with input, output, and a key.publicz'usage: %%prog [options] %(keyname)s_key decrypt	decrypted
decryptingzEName of the file to %(operation)s. Reads from stdin if not specified.zjName of the file to write the %(operation_past)s file to. Written to stdout if this option is not present.r   Tr   Nc                 C   s4   | j | jj | _ | j| jj | _| j| jj | _d S )N)r   	__class____dict__
input_helpoutput_help)selfr1   r1   r2   __init__w   s   zCryptoOperation.__init__indatakeyr+   c                 C   s   dS )zPerforms the program's operation.

        Implement in a subclass.

        :returns: the data to write to the output.
        Nr1   )r>   r@   rA   r+   r1   r1   r2   perform_operation|   s    z!CryptoOperation.perform_operationc                 C   sh   |   \}}| |d |j}| |j}t| j tj	d | 
|||}| jr2| ||j dS dS )zRuns the program.r   r   N)	parse_cliread_keykeyformread_infileinputr    operation_progressivetitler   r!   rB   
has_outputwrite_outfileoutput)r>   r*   r+   rA   r@   outdatar1   r1   r2   __call__   s   zCryptoOperation.__call__c                 C   s   t j| j| jd}|jddd| jd | jr |jddd| jd |jdd	| j d
dd |	t
jdd \}}t|| jkrG|  td||fS )zFParse the CLI options

        :returns: (cli_opts, cli_args)
        r   z-iz--inputr   r   r
   z--outputz	--keyformz&Key format of the %s key - default PEMr   r   r   r   N)r   r   r   r   r   r<   rJ   r=   keynamer   r   r   r   expected_cli_argsr   r   )r>   r)   r*   r+   r1   r1   r2   rC      s   zCryptoOperation.parse_clifilenamerE   c                 C   sX   t d| j|f tjd t|d}| }W d   n1 s w   Y  | j||S )zReads a public or private key.zReading %s key from %sr   rbN)r    rO   r   r!   r$   read	key_classZ
load_pkcs1)r>   rQ   rE   keyfileZkeydatar1   r1   r2   rD      s
   
zCryptoOperation.read_keyinnamec                 C   sb   |r$t d| tjd t|d}| W  d   S 1 sw   Y  t dtjd tjj S )zRead the input filezReading input from %sr   rR   NzReading input from stdin)r    r   r!   r$   rS   stdinr(   )r>   rV   infiler1   r1   r2   rF      s    zCryptoOperation.read_infilerM   outnamec                 C   sp   |r(t d| tjd t|d}|| W d   dS 1 s!w   Y  dS t dtjd tjj| dS )zWrite the output filezWriting output to %sr   r   NzWriting output to stdout)r    r   r!   r$   r%   r'   r(   )r>   rM   rY   r0   r1   r1   r2   rK      s   "zCryptoOperation.write_outfiler   N)%__name__
__module____qualname____doc__rO   r   r   	operationoperation_pastrH   r<   r=   rP   rJ   r"   	PublicKeyrT   r?   abcabstractmethodbytesrA   AbstractKey	IndexabletypingAnyrB   rN   Tupler   ValuesListstrrC   rD   rF   rK   r1   r1   r1   r2   r4   d   s<    


 	r4   )	metaclassc                	   @   sF   e Zd ZdZdZdZdZdZdZ	dde	d	e
jjd
ede	fddZdS )EncryptOperationzEncrypts a file.r5   zWEncrypts a file. The file must be shorter than the key length in order to be encrypted.encryptZ	encryptedZ
encryptingr1   r@   r-   r+   r   c                 C      t |tjjs	J t||S )zEncrypts files.)
isinstancer"   rA   ra   ro   )r>   r@   r-   r+   r1   r1   r2   rB         z"EncryptOperation.perform_operationNr1   )r[   r\   r]   r^   rO   r   r_   r`   rH   rd   r"   rA   re   rf   rB   r1   r1   r1   r2   rn      s$    rn   c                	   @   sL   e Zd ZdZdZdZdZdZdZe	j
Z	dded	e	jjd
edefddZdS )DecryptOperationzDecrypts a file.privatezgDecrypts a file. The original file must be shorter than the key length in order to have been encrypted.r7   r8   r9   r1   r@   r.   r+   r   c                 C   rp   )zDecrypts files.)rq   r"   rA   
PrivateKeyr7   )r>   r@   r.   r+   r1   r1   r2   rB      rr   z"DecryptOperation.perform_operationNrs   )r[   r\   r]   r^   rO   r   r_   r`   rH   r"   rv   rT   rd   rA   re   rf   rB   r1   r1   r1   r2   rt      s&    rt   c                   @   s^   e Zd ZdZdZdZdde ZdZ	dZ
dZejZd	Zd
ZdedejjdedefddZdS )SignOperationzSigns a file.ru   z/usage: %%prog [options] private_key hash_methodzCSigns a file, outputs the signature. Choose the hash method from %s, sign	signatureZSigning   z\Name of the file to write the signature to. Written to stdout if this option is not present.r@   r.   r+   r   c                 C   sB   t |tjjs	J |d }|tvrtddt t|||S )zSigns files.r   z%Invalid hash method, choose one of %srx   )rq   r"   rA   rv   HASH_METHODSr   joinry   )r>   r@   r.   r+   Zhash_methodr1   r1   r2   rB     s
   zSignOperation.perform_operationN)r[   r\   r]   r^   rO   r   r}   r|   r   r_   r`   rH   r"   rv   rT   rP   r=   rd   rA   re   rf   rB   r1   r1   r1   r2   rw      s0    rw   c                   @   sT   e Zd ZdZdZdZdZdZdZdZ	e
jZdZd	Zd
ede
jjdeddfddZdS )VerifyOperationzVerify a signature.r5   z1usage: %%prog [options] public_key signature_filezsVerifies a signature, exits with status 0 upon success, prints an error message and exits with status 1 upon error.verifyZverifiedZ	Verifyingr{   Fr@   r-   r+   r   Nc              
   C   s   t |tjjs	J |d }t|d}| }W d   n1 s!w   Y  z	t||| W n tjyA } ztd|d}~ww t	dt
jd dS )zVerifies files.r   rR   NzVerification failed.zVerification OKr   )rq   r"   rA   ra   r$   rS   r   ZVerificationErrorr   r    r   r!   )r>   r@   r-   r+   Zsignature_fileZsigfilerz   r,   r1   r1   r2   rB   +  s   

z!VerifyOperation.perform_operation)r[   r\   r]   r^   rO   r   r   r_   r`   rH   r"   ra   rT   rP   rJ   rd   rA   re   rf   rB   r1   r1   r1   r2   r~     s*    r~   rZ   )r^   rb   r   rg   r   r"   Zrsa.keyZ	rsa.pkcs1sortedZpkcs1r|   keysUnionri   rk   rl   rf   r3   ABCMetar4   rn   rt   rw   r~   ro   r7   ry   r   r1   r1   r1   r2   <module>   s(   
Cn!#
