o
    ýÞ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„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r£td|j tjd� |j|jd�}t|jdƒ�}| |¡ W d  ƒ n1 sžw   Y  |j|jd�}|jrÕtd|j tjd� t|jdƒ�}| |¡ W d  ƒ dS 1 sÎw   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ÚstringzžOutput 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   úG/var/www/html/premium_crap/venv/lib/python3.10/site-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 )z€Performs 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!#
