U
    ڲg'                     @   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_pkcs1formopenwriteoutstdoutbuffer)	parserclicli_argsZkeysizeexpub_keypriv_keydataoutfile r2   +/tmp/pip-unpacked-wheel-1z1vrokf/rsa/cli.pykeygen!   sZ    r4   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)selfr2   r2   r3   __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.
        Nr2   )r?   rA   rB   r,   r2   r2   r3   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"   rC   
has_outputwrite_outfileoutput)r?   r+   r,   rB   rA   outdatar2   r2   r3   __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=   rK   r>   keynamer   r   r   r   expected_cli_argsr   r   )r?   r*   r+   r,   r2   r2   r3   rD      s    zCryptoOperation.parse_cli)filenamerF   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!   rP   r   r"   r%   read	key_classZ
load_pkcs1)r?   rR   rF   keyfileZkeydatar2   r2   r3   rE      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   rS   zReading input from stdin)r!   r   r"   r%   rT   stdinr)   )r?   rW   infiler2   r2   r3   rG      s    zCryptoOperation.read_infile)rN   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?   rN   rZ   r1   r2   r2   r3   rL      s    zCryptoOperation.write_outfile)%__name__
__module____qualname____doc__rP   r   r   	operationoperation_pastrI   r=   r>   rQ   rK   r#   	PublicKeyrU   r@   abcabstractmethodbytesrB   AbstractKey	IndexabletypingAnyrC   rO   Tupler   ValuesListstrrD   rE   rG   rL   r2   r2   r2   r3   r5   d   s2     
 	r5   )	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.r6   zWEncrypts a file. The file must be shorter than the key length in order to be encrypted.encryptZ	encryptedZ
encryptingr2   rA   r.   r,   r   c                 C   s   t |tjjstt||S )zEncrypts files.)
isinstancer#   rB   ra   AssertionErrorro   )r?   rA   r.   r,   r2   r2   r3   rC      s    z"EncryptOperation.perform_operationN)r2   )r[   r\   r]   r^   rP   r   r_   r`   rI   rd   r#   rB   re   rf   rC   r2   r2   r2   r3   rn      s      rn   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.r8   r9   r:   r2   rA   r/   r,   r   c                 C   s   t |tjjstt||S )zDecrypts files.)rq   r#   rB   
PrivateKeyrr   r8   )r?   rA   r/   r,   r2   r2   r3   rC      s    z"DecryptOperation.perform_operationN)r2   )r[   r\   r]   r^   rP   r   r_   r`   rI   r#   rv   rU   rd   rB   re   rf   rC   r2   r2   r2   r3   rs      s      rs   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.rt   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.ru   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 %srx   )	rq   r#   rB   rv   rr   HASH_METHODSr   joinry   )r?   rA   r/   r,   Zhash_methodr2   r2   r3   rC     s
    zSignOperation.perform_operationN)r[   r\   r]   r^   rP   r   r}   r|   r   r_   r`   rI   r#   rv   rU   rQ   r>   rd   rB   re   rf   rC   r2   r2   r2   r3   rw      s&     rw   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.r6   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{   FNrp   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   rS   zVerification failed.NzVerification OKr   )rq   r#   rB   ra   rr   r%   rT   r   ZVerificationErrorr   r!   r   r"   )r?   rA   r.   r,   Zsignature_fileZsigfilerz   r-   r2   r2   r3   rC   +  s    z!VerifyOperation.perform_operation)r[   r\   r]   r^   rP   r   r   r_   r`   rI   r#   ra   rU   rQ   rK   rd   rB   re   rf   rC   r2   r2   r2   r3   r~     s      r~   )r^   rb   r   rg   r   r#   Zrsa.keyZ	rsa.pkcs1sortedZpkcs1r|   keysUnionri   rk   rl   rf   r4   ABCMetar5   rn   rs   rw   r~   ro   r8   ry   r   r2   r2   r2   r3   <module>   s&   Cn!#