a
    db/                     @   s  d Z ddlmZmZ ddlZddlZddlmZ ddlZddl	Zddl
Zeejj Zd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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e Ze ZdS )zWCommandline scripts.

These scripts are called by the executables defined in setup.py.
    )with_statementprint_functionN)OptionParserc                  C   s  t 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rt|   tdzt|d }W n8 t	y   |   t
d|d  tjd tdY n0 t
d| tjd t|\}}|jr@t
d|j tjd |j|jd}t|jd}|| W d   n1 s60    Y  |j|jd}|jrt
d|j tjd t|jd}|| W d   n1 s0    Y  nt
dtjd tj| dS )zKey generator.zusage: %prog [options] keysizez.Generates a new RSA keypair 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)r   
add_option
parse_argssysargvlen
print_help
SystemExitint
ValueErrorprintstderrrsaZnewkeysZpuboutZ
save_pkcs1formopenwriteoutstdout)parserclicli_argsZkeysizepub_keypriv_keydataoutfile r/   X/home/tom/ab/renpy-build/tmp/install.linux-x86_64/lib/python3.9/site-packages/rsa/cli.pykeygen#   sJ    
*,r1   c                   @   s   e Zd ZdZej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 ZejdddZdd Zdd Zdd Zdd Zdd ZdS )CryptoOperationz9CLI callable that operates with input, output, and a key.publicz'usage: %%prog [options] %(keyname)s_keyN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   Tc                 C   s4   | j | jj | _ | j| jj | _| j| jj | _d S N)r   	__class____dict__
input_helpoutput_helpselfr/   r/   r0   __init__n   s    zCryptoOperation.__init__c                 C   s   dS )zPerforms the program's operation.

        Implement in a subclass.

        :returns: the data to write to the output.
        Nr/   )r=   indatakeyr*   r/   r/   r0   perform_operations   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!   rA   
has_outputwrite_outfileoutput)r=   r)   r*   r@   r?   outdatar/   r/   r0   __call__|   s    zCryptoOperation.__call__c                 C   s   t | 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:   rJ   r;   keynamer   r   r   r   expected_cli_argsr   r   )r=   r(   r)   r*   r/   r/   r0   rC      s    zCryptoOperation.parse_clic                 C   sX   t d| j|f tjd t|d}| }W d   n1 s@0    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=   filenamerE   keyfileZkeydatar/   r/   r0   rD      s    &zCryptoOperation.read_keyc                 C   s`   |rHt d| tjd t|d}| W  d   S 1 s>0    Y  t dtjd tj S )zRead the input fileReading input from %sr   rQ   NReading input from stdin)r    r   r!   r$   rR   stdin)r=   innameinfiler/   r/   r0   rF      s    &zCryptoOperation.read_infilec                 C   sj   |rLt d| tjd t|d}|| W d   qf1 s@0    Y  nt dtjd t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=   rM   outnamer.   r/   r/   r0   rK      s    *zCryptoOperation.write_outfile)N)__name__
__module____qualname____doc__abcABCMeta__metaclass__rO   r   r   	operationoperation_pastrH   r:   r;   rP   rJ   r"   	PublicKeyrS   r>   abstractmethodrA   rN   rC   rD   rF   rK   r/   r/   r/   r0   r2   Z   s*   	r2   c                   @   s.   e Zd ZdZdZdZdZdZdZd
dd	Z	dS )EncryptOperationzEncrypts a file.r3   zEncrypts a file. The file must be shorter than the key length in order to be encrypted. For larger files, use the pyrsa-encrypt-bigfile command.encrypt	encrypted
encryptingNc                 C   s   t ||S )zEncrypts files.)r"   rh   )r=   r?   r+   r*   r/   r/   r0   rA      s    z"EncryptOperation.perform_operation)N
r\   r]   r^   r_   rO   r   rc   rd   rH   rA   r/   r/   r/   r0   rg      s   rg   c                   @   s4   e Zd ZdZdZdZdZdZdZe	j
Zd
dd	ZdS )DecryptOperationzDecrypts a file.privatezDecrypts a file. The original file must be shorter than the key length in order to have been encrypted. For larger files, use the pyrsa-decrypt-bigfile command.r4   r5   r6   Nc                 C   s   t ||S )zDecrypts files.)r"   r4   )r=   r?   r,   r*   r/   r/   r0   rA      s    z"DecryptOperation.perform_operation)Nr\   r]   r^   r_   rO   r   rc   rd   rH   r"   
PrivateKeyrS   rA   r/   r/   r/   r0   rl      s   rl   c                   @   sH   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d ZdS )SignOperationzSigns a file.rm   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.c                 C   s0   |d }|t vr"tddt  t|||S )zSigns files.r   z%Invalid hash method, choose one of %srq   )HASH_METHODSr   joinr"   rr   )r=   r?   r,   r*   Zhash_methodr/   r/   r0   rA      s    zSignOperation.perform_operationN)r\   r]   r^   r_   rO   r   rv   ru   r   rc   rd   rH   r"   ro   rS   rP   r;   rA   r/   r/   r/   r0   rp      s   rp   c                   @   s>   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
d ZdS )VerifyOperationzVerify a signature.r3   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	Verifyingrt   Fc                 C   s|   |d }t |d}| }W d   n1 s00    Y  zt||| W n tjyh   tdY n0 tdtjd dS )zVerifies files.r   rQ   NzVerification failed.zVerification OKr   )	r$   rR   r"   rx   ZVerificationErrorr   r    r   r!   )r=   r?   r+   r*   Zsignature_fileZsigfilers   r/   r/   r0   rA     s    &z!VerifyOperation.perform_operationN)r\   r]   r^   r_   rO   r   r   rc   rd   rH   r"   re   rS   rP   rJ   rA   r/   r/   r/   r0   rw     s   rw   c                   @   s8   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d ZdS )BigfileOperationz>CryptoOperation that doesn't read the entire file into memory.c                 C   s   t |  g | _d S r7   )r2   r>   file_objectsr<   r/   r/   r0   r>   #  s    
zBigfileOperation.__init__c                 C   s   | j D ]}|  qdS )zCloses any open file handles.N)rz   close)r=   fobjr/   r/   r0   __del__(  s    
zBigfileOperation.__del__c                 C   s^   |   \}}| |d |j}| |j}| |j}t| j	 t
jd | |||| dS rB   )rC   rD   rE   
get_infilerG   get_outfilerL   r    rH   rI   r   r!   rA   )r=   r)   r*   r@   rZ   r.   r/   r/   r0   rN   .  s    zBigfileOperation.__call__c                 C   sF   |r.t d| tjd t|d}| j| nt dtjd tj}|S )zReturns the input file objectrV   r   rQ   rW   )r    r   r!   r$   rz   appendrX   )r=   rY   r|   r/   r/   r0   r~   =  s    
zBigfileOperation.get_infilec                 C   sF   |r.t d| tjd t|d}| j| nt dtjd tj}|S )zReturns the output file objectzWill write output to %sr   r   zWill write output to stdout)r    r   r!   r$   rz   r   r'   )r=   r[   r|   r/   r/   r0   r   J  s    
zBigfileOperation.get_outfileN)	r\   r]   r^   r_   r>   r}   rN   r~   r   r/   r/   r/   r0   ry      s   ry   c                   @   s.   e Zd ZdZdZdZdZdZdZd
dd	Z	dS )EncryptBigfileOperationz#Encrypts a file to VARBLOCK format.r3   zEncrypts a file to an encrypted VARBLOCK file. The file can be larger than the key length, but the output file is only compatible with Python-RSA.rh   ri   rj   Nc                 C   s   t j|||S )zEncrypts files to VARBLOCK.)r"   bigfileencrypt_bigfile)r=   rZ   r.   r+   r*   r/   r/   r0   rA   c  s    z)EncryptBigfileOperation.perform_operation)Nrk   r/   r/   r/   r0   r   X  s   r   c                   @   s4   e Zd ZdZdZdZdZdZdZe	j
Zd
dd	ZdS )DecryptBigfileOperationz#Decrypts a file in VARBLOCK format.rm   zQDecrypts an encrypted VARBLOCK file that was encrypted with pyrsa-encrypt-bigfiler4   r5   r6   Nc                 C   s   t j|||S )zDecrypts a VARBLOCK file.)r"   r   decrypt_bigfile)r=   rZ   r.   r,   r*   r/   r/   r0   rA   t  s    z)DecryptBigfileOperation.perform_operation)Nrn   r/   r/   r/   r0   r   i  s   r   )r_   
__future__r   r   r`   r   optparser   r"   Zrsa.bigfileZ	rsa.pkcs1sortedZpkcs1ru   keysr1   objectr2   rg   rl   rp   rw   ry   r   r   rh   r4   rr   rx   r   r   r/   r/   r/   r0   <module>   s.   7j8