o
    <a'                     @   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_pkcs1Zformopenwriteoutstdoutbuffer)	parserclicli_argsZkeysizeexpub_keypriv_keydataoutfile r0   +usr/lib/python3.10/site-packages/rsa/cli.pykeygen!   sb   
"r2   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)selfr0   r0   r1   __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.
        Nr0   )r=   r?   r@   r*   r0   r0   r1   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!   rA   
has_outputwrite_outfileoutput)r=   r)   r*   r@   r?   outdatar0   r0   r1   __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;   rI   r<   keynamer   r   r   r   expected_cli_argsr   r   )r=   r(   r)   r*   r0   r0   r1   rB      s   zCryptoOperation.parse_clifilenamerD   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    rN   r   r!   r#   read	key_classZ
load_pkcs1)r=   rP   rD   ZkeyfileZkeydatar0   r0   r1   rC      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   rQ   NzReading input from stdin)r    r   r!   r#   rR   stdinr'   )r=   rT   infiler0   r0   r1   rE      s    zCryptoOperation.read_infilerL   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=   rL   rW   r/   r0   r0   r1   rJ      s   "zCryptoOperation.write_outfiler   N)%__name__
__module____qualname____doc__rN   r   r   	operationoperation_pastrG   r;   r<   rO   rI   r"   	PublicKeyrS   r>   abcabstractmethodbytesr@   AbstractKey	IndexabletypingAnyrA   rM   Tupler   ZValuesListstrrB   rC   rE   rJ   r0   r0   r0   r1   r3   d   s<    


 	r3   )	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.r4   zWEncrypts a file. The file must be shorter than the key length in order to be encrypted.encryptZ	encryptedZ
encryptingr0   r?   r,   r*   r   c                 C      t |tjjs	J t||S )zEncrypts files.)
isinstancer"   r@   r_   rl   )r=   r?   r,   r*   r0   r0   r1   rA         z"EncryptOperation.perform_operationNr0   )rY   rZ   r[   r\   rN   r   r]   r^   rG   rb   r"   r@   rc   rd   rA   r0   r0   r0   r1   rk      s$    rk   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.r6   r7   r8   r0   r?   r-   r*   r   c                 C   rm   )zDecrypts files.)rn   r"   r@   
PrivateKeyr6   )r=   r?   r-   r*   r0   r0   r1   rA      ro   z"DecryptOperation.perform_operationNrp   )rY   rZ   r[   r\   rN   r   r]   r^   rG   r"   rs   rS   rb   r@   rc   rd   rA   r0   r0   r0   r1   rq      s&    rq   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.rr   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 %sru   )rn   r"   r@   rs   HASH_METHODSr   joinrv   )r=   r?   r-   r*   Zhash_methodr0   r0   r1   rA     s
   zSignOperation.perform_operationN)rY   rZ   r[   r\   rN   r   rz   ry   r   r]   r^   rG   r"   rs   rS   rO   r<   rb   r@   rc   rd   rA   r0   r0   r0   r1   rt      s0    rt   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.r4   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	Verifyingrx   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   rQ   NzVerification failed.zVerification OKr   )rn   r"   r@   r_   r#   rR   r|   ZVerificationErrorr   r    r   r!   )r=   r?   r,   r*   Zsignature_fileZsigfilerw   r+   r0   r0   r1   rA   +  s   

z!VerifyOperation.perform_operation)rY   rZ   r[   r\   rN   r   r   r]   r^   rG   r"   r_   rS   rO   rI   rb   r@   rc   rd   rA   r0   r0   r0   r1   r{     s*    r{   rX   )r\   r`   r   re   r   r"   Zrsa.keyZ	rsa.pkcs1sortedZpkcs1ry   keysUnionrg   rh   ri   rd   r2   ABCMetar3   rk   rq   rt   r{   rl   r6   rv   r|   r0   r0   r0   r1   <module>   s(   
Cn!#
