
    g'                        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                  j                               Z
ej                  ej                  ej                  e   f   ZddZ G d dej$                        Z G d de      Z G d	 d
e      Z G d de      Z G d de      Z e       Z e       Z e       Z e       Zy)zWCommandline scripts.

These scripts are called by the executables defined in setup.py.
    Nc                  ,   t        j                  dd      } | j                  ddd       | j                  dd	dd
       | j                  dddd       | j                  t        j
                  dd       \  }}t        |      dk7  r| j                          t        d      	 t        |d         }t        d|z  t        j                         t        j                  |      \  }}|j                  rtt        d|j                  z  t        j                         |j!                  |j"                        }t%        |j                  d      5 }|j'                  |       ddd       |j!                  |j"                        }|j(                  rYt        d|j(                  z  t        j                         t%        |j(                  d      5 }|j'                  |       ddd       yt        dt        j                         t        j*                  j,                  j'                  |       y# t        $ rB}| j                          t        d|d   z  t        j                         t        d      |d}~ww xY w# 1 sw Y   xY w# 1 sw Y   yxY w)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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outfiles	            </var/www/openai/venv/lib/python3.12/site-packages/rsa/cli.pykeygenr8   !   s.    "".EF
 >   ;	   F	   ''5OS(
8}m$hqk" 

!G
+#**=++g.Wh zz(3::5CJJG!!!2#**d#wMM$ $ chh/D
ww)CGG3#**E#''4 GMM$ !  	-CJJ?

%1  $&!43::Fm#$ $# ! s0   %H/ I=J
/	I:8=I55I:=J
Jc            	          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Zej&                  dedej*                  j,                  dedej2                  fd       ZddZdej8                  ej<                  ej>                  e    f   fdZ!de de dej*                  j,                  fdZ"de defdZ#dede ddfdZ$y)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   TreturnNc                     | j                   | j                  j                  z  | _         | j                  | j                  j                  z  | _        | j                  | j                  j                  z  | _        y )N)r   	__class____dict__
input_helpoutput_help)selfs    r7   __init__zCryptoOperation.__init__w   sS    ZZ$.."9"99
//DNN,C,CC++dnn.E.EE    indatakeyr0   c                      y)zPerforms the program's operation.

        Implement in a subclass.

        :returns: the data to write to the output.
        N )rF   rI   rJ   r0   s       r7   perform_operationz!CryptoOperation.perform_operation|   s    rH   c                 |   | j                         \  }}| j                  |d   |j                        }| j                  |j                        }t        | j                  j                         t        j                         | j                  |||      }| j                  r| j                  ||j                         yy)zRuns the program.r   r   N)	parse_cliread_keykeyformread_infileinputr"   operation_progressivetitler   r#   rM   
has_outputwrite_outfileoutput)rF   r/   r0   rJ   rI   outdatas         r7   __call__zCryptoOperation.__call__   s     ..*hmmHQK5!!#)),d((..0szzB((h???w

3 rH   c                    t        j                  | j                  | j                        }|j	                  ddd| j
                         | j                  r|j	                  ddd| j                         |j	                  dd	| j                  z  d
d       |j                  t        j                  dd       \  }}t        |      | j                  k7  r|j                          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   rD   rV   rE   keynamer   r   r   r   expected_cli_argsr   r   )rF   r.   r/   r0   s       r7   rO   zCryptoOperation.parse_cli   s     &&TZZTEUEUV$	tO??dJXDDTDTU9DLLH"	 	 	
 !++CHHQRL9hx=D222Q-H}rH   filenamerQ   c                     t        d| j                  d|t        j                         t	        |d      5 }|j                         }ddd       | j                  j                  |      S # 1 sw Y   %xY w)zReads a public or private key.zReading z
 key from r   rbN)r"   r\   r   r#   r)   read	key_class
load_pkcs1)rF   r^   rQ   keyfilekeydatas        r7   rP   zCryptoOperation.read_key   sY     	$,,A

S(D!WllnG " ~~((':: "!s   A--A6innamec                 ,   |rDt        d|z  t        j                         t        |d      5 }|j	                         cddd       S t        dt        j                         t        j
                  j                  j	                         S # 1 sw Y   LxY w)zRead the input filezReading input from %sr   r`   NzReading input from stdin)r"   r   r#   r)   ra   stdinr-   )rF   rf   infiles      r7   rR   zCryptoOperation.read_infile   sk     )F2Dfd#v{{} $# 	(szz:yy$$&&	 $#s   B

BrY   outnamec                 2   |rEt        d|z  t        j                         t        |d      5 }|j	                  |       ddd       yt        dt        j                         t        j
                  j                  j	                  |       y# 1 sw Y   yxY w)zWrite the output filezWriting output to %sr   r   NzWriting output to stdout)r"   r   r#   r)   r*   r,   r-   )rF   rY   rj   r6   s       r7   rW   zCryptoOperation.write_outfile   sm     (72Dgt$g& %$ ,3::>JJ##G,	 %$s   BBr@   N)%__name__
__module____qualname____doc__r\   r   r   	operationoperation_pastrT   rD   rE   r]   rV   r$   	PublicKeyrb   rG   abcabstractmethodbytesrJ   AbstractKey	IndexabletypingAnyrM   rZ   Tupler   ValuesListstrrO   rP   rR   rW   rL   rH   r7   r:   r:   d   s   CG5EKI N([J	?  JIF
 	"%''"5"5AJ	 46<<S9I(IJ 8; ;s ;sww7J7J ;	'# 	'% 	'	-U 	-S 	-T 	-rH   r:   )	metaclassc            	       h    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Zy)EncryptOperationzEncrypts a file.r;   zWEncrypts a file. The file must be shorter than the key length in order to be encrypted.encrypt	encrypted
encryptingrI   r3   r0   r@   c                 z    t        |t        j                  j                        sJ t        j                  ||      S )zEncrypts files.)
isinstancer$   rJ   rs   r   )rF   rI   r3   r0   s       r7   rM   z"EncryptOperation.perform_operation   s/     '377#4#4555{{67++rH   NrL   )rm   rn   ro   rp   r\   r   rq   rr   rT   rv   r$   rJ   rw   rx   rM   rL   rH   r7   r   r      sZ    Gd  I N( RT,,&)gg&9&9,EN,	,rH   r   c            	           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Zy)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?   rI   r4   r0   r@   c                 z    t        |t        j                  j                        sJ t        j                  ||      S )zDecrypts files.)r   r$   rJ   
PrivateKeyr=   )rF   rI   r4   r0   s       r7   rM   z"DecryptOperation.perform_operation   s/     (CGG$6$6777{{68,,rH   Nr   )rm   rn   ro   rp   r\   r   rq   rr   rT   r$   r   rb   rv   rJ   rw   rx   rM   rL   rH   r7   r   r      se    G	:  I N(I SU--'*ww':':-FO-	-rH   r   c                       e Zd ZdZdZdZddj                  e      z  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Zy)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.rI   r4   r0   r@   c                     t        |t        j                  j                        sJ |d   }|t        vr!t        ddj                  t              z        t        j                  |||      S )zSigns files.r   z%Invalid hash method, choose one of %sr   )r   r$   rJ   r   HASH_METHODSr   joinr   )rF   rI   r4   r0   hash_methods        r7   rM   zSignOperation.perform_operation  s]     (CGG$6$6777qkl*DtyyQ]G^^__xx+66rH   N)rm   rn   ro   rp   r\   r   r   r   r   rq   rr   rT   r$   r   rb   r]   rE   rv   rJ   rw   rx   rM   rL   rH   r7   r   r      s    G=E	99\2	3  I N%I	3 

7
7'*ww':':
7FO
7	
7rH   r   c                       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Zy)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   FrI   r3   r0   r@   Nc                 p   t        |t        j                  j                        sJ |d   }t	        |d      5 }|j                         }ddd       	 t        j                  ||       t        dt        j                         y# 1 sw Y   =xY w# t        j                  $ r}t        d      |d}~ww xY w)zVerifies files.r   r`   NzVerification failed.zVerification OKr   )r   r$   rJ   rs   r)   ra   r   VerificationErrorr   r"   r   r#   )rF   rI   r3   r0   signature_filesigfiler   r2   s           r7   rM   z!VerifyOperation.perform_operation+  s     '377#4#4555!!.$'7I (	=JJvy'2 	cjj1 ('
 $$ 	=34"<	=s#   BB BB5$B00B5)rm   rn   ro   rp   r\   r   r   rq   rr   rT   r$   rs   rb   r]   rV   rv   rJ   rw   rx   rM   rL   rH   r7   r   r     so    G?E	F  IN'IJ22&)gg&9&92EN2	2rH   r   rl   )rp   rt   r   ry   r   r$   rsa.key	rsa.pkcs1sortedpkcs1r   keysUnionr{   r}   r~   rx   r8   ABCMetar:   r   r   r   r   r   r=   r   r   rL   rH   r7   <module>r      s   
  
   
  cii,,1134LLv{{3'778	@&Fk- k-\, ,&- -*7O 7B 2o  2F 


		rH   