o
    e'                     @   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PEM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rsanewkeyspubout
save_pkcs1formopenwriteoutstdoutbuffer)	parserclicli_argskeysizeexpub_keypriv_keydataoutfile r6   2/usr/local/lib/python3.10/dist-packages/rsa/cli.pykeygen!   sb   
"r8   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)selfr6   r6   r7   __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.
        Nr6   )rC   rE   rF   r/   r6   r6   r7   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"   rG   
has_outputwrite_outfileoutput)rC   r.   r/   rF   rE   outdatar6   r6   r7   __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   rA   rO   rB   keynamer   r   r   r   expected_cli_argsr   r   )rC   r-   r.   r/   r6   r6   r7   rH      s   zCryptoOperation.parse_clifilenamerJ   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!   rT   r   r"   r(   read	key_class
load_pkcs1)rC   rV   rJ   keyfilekeydatar6   r6   r7   rI      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   rW   NzReading input from stdin)r!   r   r"   r(   rX   stdinr,   )rC   r]   infiler6   r6   r7   rK      s    zCryptoOperation.read_infilerR   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,   )rC   rR   r`   r5   r6   r6   r7   rP      s   "zCryptoOperation.write_outfiler   N)%__name__
__module____qualname____doc__rT   r   r   	operationoperation_pastrM   rA   rB   rU   rO   r#   	PublicKeyrY   rD   abcabstractmethodbytesrF   AbstractKey	IndexabletypingAnyrG   rS   Tupler   ValuesListstrrH   rI   rK   rP   r6   r6   r6   r7   r9   d   s<    


 	r9   )	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.r:   zWEncrypts a file. The file must be shorter than the key length in order to be encrypted.encrypt	encrypted
encryptingr6   rE   r2   r/   r   c                 C      t |tjjs	J t||S )zEncrypts files.)
isinstancer#   rF   rh   rv   )rC   rE   r2   r/   r6   r6   r7   rG         z"EncryptOperation.perform_operationNr6   )rb   rc   rd   re   rT   r   rf   rg   rM   rk   r#   rF   rl   rm   rG   r6   r6   r6   r7   ru      s$    ru   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.r<   r=   r>   r6   rE   r3   r/   r   c                 C   ry   )zDecrypts files.)rz   r#   rF   
PrivateKeyr<   )rC   rE   r3   r/   r6   r6   r7   rG      r{   z"DecryptOperation.perform_operationNr|   )rb   rc   rd   re   rT   r   rf   rg   rM   r#   r   rY   rk   rF   rl   rm   rG   r6   r6   r6   r7   r}      s&    r}   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.r~   z/usage: %%prog [options] private_key hash_methodzCSigns a file, outputs the signature. Choose the hash method from %s, sign	signatureSigning   z\Name of the file to write the signature to. Written to stdout if this option is not present.rE   r3   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 %sr   )rz   r#   rF   r   HASH_METHODSr   joinr   )rC   rE   r3   r/   hash_methodr6   r6   r7   rG     s
   zSignOperation.perform_operationN)rb   rc   rd   re   rT   r   r   r   r   rf   rg   rM   r#   r   rY   rU   rB   rk   rF   rl   rm   rG   r6   r6   r6   r7   r      s0    r   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.r:   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.verifyverified	Verifyingr   FrE   r2   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   rW   NzVerification failed.zVerification OKr   )rz   r#   rF   rh   r(   rX   r   VerificationErrorr   r!   r   r"   )rC   rE   r2   r/   signature_filesigfiler   r1   r6   r6   r7   rG   +  s   

z!VerifyOperation.perform_operation)rb   rc   rd   re   rT   r   r   rf   rg   rM   r#   rh   rY   rU   rO   rk   rF   rl   rm   rG   r6   r6   r6   r7   r     s*    r   ra   )re   ri   r   rn   r   r#   rsa.key	rsa.pkcs1sortedpkcs1r   keysUnionrp   rr   rs   rm   r8   ABCMetar9   ru   r}   r   r   rv   r<   r   r   r6   r6   r6   r7   <module>   s(   
Cn!#
