U
    ʹh&                     @   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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rv|   tdzt	|d }W nJ t
k
r } z,|   td|d  tjd td|W 5 d}~X Y nX td| tjd t|\}}|jr>td|j tjd |j|jd}t|jd}|| W 5 Q R X |j|jd}|jrtd|j tjd t|jd}|| W 5 Q R X n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 r1   -./venv/lib/python3.8/site-packages/rsa/cli.pykeygen!   sZ    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dZejeejjeejdddZddddZejejeje  f dddZ!e e ejjdddZ"e edddZ#ee dd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   TNr   c                 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+   r   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   sd   |   \}}| |d |j}| |j}t| j tj	d | 
|||}| jr`| ||j 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r|  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   r   c              	   C   sD   t d| j|f tjd t|d}| }W 5 Q R X | j||S )zReads a public or private key.zReading %s key from %sr   rb)r!   rO   r   r"   r$   read	key_classZ
load_pkcs1)r>   rQ   rE   ZkeyfileZkeydatar1   r1   r2   rD      s    zCryptoOperation.read_key)innamer   c              
   C   sV   |r<t d| tjd t|d}| W  5 Q R  S Q R X t dtjd tjj S )zRead the input filezReading input from %sr   rR   zReading input from stdin)r!   r   r"   r$   rS   stdinr(   )r>   rU   Zinfiler1   r1   r2   rF      s    zCryptoOperation.read_infile)rM   outnamer   c              	   C   sX   |r8t d| tjd t|d}|| W 5 Q R X nt dtjd tjj| dS )zWrite the output filezWriting output to %sr   r   zWriting output to stdoutN)r!   r   r"   r$   r%   r'   r(   )r>   rM   rW   r0   r1   r1   r2   rK      s    zCryptoOperation.write_outfile)%__name__
__module____qualname____doc__rO   r   r   	operationoperation_pastrH   r<   r=   rP   rJ   r#   	PublicKeyrT   r?   abcabstractmethodbytesrA   AbstractKey	IndexabletypingZAnyrB   rN   Tupler   ZValuesListstrrC   rD   rF   rK   r1   r1   r1   r2   r4   d   s2     
 	r4   )	metaclassc                   @   s>   e Zd ZdZdZdZdZdZdZde	e
jjee	d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   s   t |tjjstt||S )zEncrypts files.)
isinstancer#   rA   r^   AssertionErrorrj   )r>   r@   r-   r+   r1   r1   r2   rB      s    z"EncryptOperation.perform_operationN)r1   )rX   rY   rZ   r[   rO   r   r\   r]   rH   ra   r#   rA   rb   rc   rB   r1   r1   r1   r2   ri      s      ri   c                   @   sD   e Zd ZdZdZdZdZdZdZe	j
Zdee	jjeed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   s   t |tjjstt||S )zDecrypts files.)rl   r#   rA   
PrivateKeyrm   r7   )r>   r@   r.   r+   r1   r1   r2   rB      s    z"DecryptOperation.perform_operationN)r1   )rX   rY   rZ   r[   rO   r   r\   r]   rH   r#   rq   rT   ra   rA   rb   rc   rB   r1   r1   r1   r2   rn      s      rn   c                   @   sX   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eejjeedddZdS )SignOperationzSigns a file.ro   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.rp   c                 C   sB   t |tjjst|d }|tkr4tddt t|||S )zSigns files.r   z%Invalid hash method, choose one of %srs   )	rl   r#   rA   rq   rm   HASH_METHODSr   joinrt   )r>   r@   r.   r+   Zhash_methodr1   r1   r2   rB     s
    zSignOperation.perform_operationN)rX   rY   rZ   r[   rO   r   rx   rw   r   r\   r]   rH   r#   rq   rT   rP   r=   ra   rA   rb   rc   rB   r1   r1   r1   r2   rr      s&     rr   c                   @   sN   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ee
jjed
d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	Verifyingrv   FNrk   c              
   C   s   t |tjjst|d }t|d}| }W 5 Q R X zt||| W n. tjk
rx } zt	d|W 5 d}~X Y nX t
dtjd dS )zVerifies files.r   rR   zVerification failed.NzVerification OKr   )rl   r#   rA   r^   rm   r$   rS   rz   ZVerificationErrorr   r!   r   r"   )r>   r@   r-   r+   Zsignature_fileZsigfileru   r,   r1   r1   r2   rB   +  s    z!VerifyOperation.perform_operation)rX   rY   rZ   r[   rO   r   r   r\   r]   rH   r#   r^   rT   rP   rJ   ra   rA   rb   rc   rB   r1   r1   r1   r2   ry     s      ry   )r[   r_   r   rd   r   r#   Zrsa.keyZ	rsa.pkcs1sortedZpkcs1rw   keysZUnionre   rf   rg   rc   r3   ABCMetar4   ri   rn   rr   ry   rj   r7   rt   rz   r1   r1   r1   r2   <module>   s&   Cn!#