ó
‚¾^Yc           @€ ss  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 j ƒ  ƒ Z d „  Z d e f d „  ƒ  YZ d e f d	 „  ƒ  YZ d
 e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ e ƒ  Z e ƒ  Z e ƒ  Z e ƒ  Z e ƒ  Z e ƒ  Z d S(   sW   Commandline scripts.

These scripts are called by the executables defined in setup.py.
iÿÿÿÿ(   t   with_statementt   print_functionN(   t   OptionParserc          C€ s"  t  d d d d ƒ }  |  j d d d d d	 ƒ|  j d
 d d d d d ƒ|  j d d d d d d d ƒ|  j t j d ƒ \ } } t | ƒ d k r° |  j ƒ  t d ƒ ‚ n  y t | d ƒ } WnB t	 k
 r|  j ƒ  t
 d | d d t j ƒt d ƒ ‚ n Xt
 d | d t j ƒt j | ƒ \ } } | j r˜t
 d | j d t j ƒ| j d | j ƒ } t | j d ƒ  } | j | ƒ Wd QXn  | j d | j ƒ } | j rût
 d | j d t j ƒt | j d ƒ  } | j | ƒ Wd QXn# t
 d d t j ƒt j j | ƒ d S(   s   Key generator.t   usages   usage: %prog [options] keysizet   descriptions.   Generates a new RSA keypair of "keysize" bits.s   --puboutt   typet   stringt   helpsž   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.s   -os   --outs`   Output filename for the private key. The key is written to stdout if this option is not present.s   --forms7   key format of the private and public keys - default PEMt   choicest   PEMt   DERt   defaulti   i    s   Not a valid number: %st   files   Generating %i-bit keys   Writing public key to %st   formatt   wbNs   Writing private key to %ss   Writing private key to stdout(   R	   R
   (   R   t
   add_optiont
   parse_argst   syst   argvt   lent
   print_helpt
   SystemExitt   intt
   ValueErrort   printt   stderrt   rsat   newkeyst   puboutt
   save_pkcs1t   formt   opent   writet   outt   stdout(   t   parsert   clit   cli_argst   keysizet   pub_keyt   priv_keyt   datat   outfile(    (    s$   /tmp/pip-build-kpPAdC/rsa/rsa/cli.pyt   keygen#   sB    	

		t   CryptoOperationc           B€ s§   e  Z d  Z e j Z d Z d Z d Z	 d Z
 d Z d Z d Z d Z d Z e Z e j Z d	 „  Z e j d d
 „ ƒ Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   s9   CLI callable that operates with input, output, and a key.t   publics'   usage: %%prog [options] %(keyname)s_keyt   decryptt	   decryptedt
   decryptingsE   Name of the file to %(operation)s. Reads from stdin if not specified.sj   Name of the file to write the %(operation_past)s file to. Written to stdout if this option is not present.i   c         C€ sF   |  j  |  j j |  _  |  j |  j j |  _ |  j |  j j |  _ d  S(   N(   R   t	   __class__t   __dict__t
   input_helpt   output_help(   t   self(    (    s$   /tmp/pip-build-kpPAdC/rsa/rsa/cli.pyt   __init__n   s    c         C€ s   d S(   s€   Performs the program's operation.

        Implement in a subclass.

        :returns: the data to write to the output.
        N(    (   R5   t   indatat   keyR%   (    (    s$   /tmp/pip-build-kpPAdC/rsa/rsa/cli.pyt   perform_operations   s    c         C€ s‘   |  j  ƒ  \ } } |  j | d | j ƒ } |  j | j ƒ } t |  j j ƒ  d t j	 ƒ|  j
 | | | ƒ } |  j r |  j | | j ƒ n  d S(   s   Runs the program.i    R   N(   t	   parse_clit   read_keyt   keyformt   read_infilet   inputR   t   operation_progressivet   titleR   R   R9   t
   has_outputt   write_outfilet   output(   R5   R$   R%   R8   R7   t   outdata(    (    s$   /tmp/pip-build-kpPAdC/rsa/rsa/cli.pyt   __call__|   s    	c         C€ sß   t  d |  j d |  j ƒ } | j d d d d d |  j ƒ|  j re | j d d	 d d d |  j ƒn  | j d
 d d |  j d d d d ƒ| j t	 j
 d ƒ \ } } t | ƒ |  j k rÕ | j ƒ  t d ƒ ‚ n  | | f S(   sF   Parse the CLI options

        :returns: (cli_opts, cli_args)
        R   R   s   -is   --inputR   R   R   s   -os   --outputs	   --keyforms&   Key format of the %s key - default PEMR   R	   R
   R   i   (   s   PEMs   DER(   R   R   R   R   R3   RA   R4   t   keynameR   R   R   R   t   expected_cli_argsR   R   (   R5   R#   R$   R%   (    (    s$   /tmp/pip-build-kpPAdC/rsa/rsa/cli.pyR:   ‹   s    	"
c         C€ sW   t  d |  j | f d t j ƒt | d ƒ  } | j ƒ  } Wd QX|  j j | | ƒ S(   s   Reads a public or private key.s   Reading %s key from %sR   t   rbN(   R   RF   R   R   R   t   readt	   key_classt
   load_pkcs1(   R5   t   filenameR<   t   keyfilet   keydata(    (    s$   /tmp/pip-build-kpPAdC/rsa/rsa/cli.pyR;   ¤   s     c         C€ sb   | rB t  d | d t j ƒt | d ƒ  } | j ƒ  SWd QXn  t  d d t j ƒt j j ƒ  S(   s   Read the input files   Reading input from %sR   RH   Ns   Reading input from stdin(   R   R   R   R   RI   t   stdin(   R5   t   innamet   infile(    (    s$   /tmp/pip-build-kpPAdC/rsa/rsa/cli.pyR=   ­   s    c         C€ sl   | rE t  d | d t j ƒt | d ƒ  } | j | ƒ Wd QXn# t  d d t j ƒt j j | ƒ d S(   s   Write the output files   Writing output to %sR   R   Ns   Writing output to stdout(   R   R   R   R   R    R"   (   R5   RD   t   outnameR*   (    (    s$   /tmp/pip-build-kpPAdC/rsa/rsa/cli.pyRB   ¸   s    N(   t   __name__t
   __module__t   __doc__t   abct   ABCMetat   __metaclass__RF   R   t   NoneR   t	   operationt   operation_pastR?   R3   R4   RG   t   TrueRA   R   t	   PublicKeyRJ   R6   t   abstractmethodR9   RE   R:   R;   R=   RB   (    (    (    s$   /tmp/pip-build-kpPAdC/rsa/rsa/cli.pyR,   Z   s*   								t   EncryptOperationc           B€ s8   e  Z d  Z d Z d Z d Z d Z d Z d d „ Z	 RS(   s   Encrypts a file.R-   s   Encrypts 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.t   encryptt	   encryptedt
   encryptingc         C€ s   t  j | | ƒ S(   s   Encrypts files.(   R   R`   (   R5   R7   R'   R%   (    (    s$   /tmp/pip-build-kpPAdC/rsa/rsa/cli.pyR9   Ï   s    N(
   RS   RT   RU   RF   R   RZ   R[   R?   RY   R9   (    (    (    s$   /tmp/pip-build-kpPAdC/rsa/rsa/cli.pyR_   Ä   s   t   DecryptOperationc           B€ sA   e  Z d  Z d Z d Z d Z d Z d Z e j	 Z
 d d „ Z RS(   s   Decrypts a file.t   privates    Decrypts 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.R.   R/   R0   c         C€ s   t  j | | ƒ S(   s   Decrypts files.(   R   R.   (   R5   R7   R(   R%   (    (    s$   /tmp/pip-build-kpPAdC/rsa/rsa/cli.pyR9   á   s    N(   RS   RT   RU   RF   R   RZ   R[   R?   R   t
   PrivateKeyRJ   RY   R9   (    (    (    s$   /tmp/pip-build-kpPAdC/rsa/rsa/cli.pyRc   Õ   s   	t   SignOperationc           B€ s]   e  Z d  Z d Z d Z d d j e ƒ Z d Z d Z	 d Z
 e j Z d Z d	 Z d
 „  Z RS(   s   Signs a file.Rd   s/   usage: %%prog [options] private_key hash_methodsC   Signs a file, outputs the signature. Choose the hash method from %ss   , t   signt	   signaturet   Signingi   s\   Name of the file to write the signature to. Written to stdout if this option is not present.c         C€ sE   | d } | t  k r2 t d d j t  ƒ ƒ ‚ n  t j | | | ƒ S(   s   Signs files.i   s%   Invalid hash method, choose one of %ss   , (   t   HASH_METHODSR   t   joinR   Rg   (   R5   R7   R(   R%   t   hash_method(    (    s$   /tmp/pip-build-kpPAdC/rsa/rsa/cli.pyR9   ÷   s
    
(   RS   RT   RU   RF   R   Rk   Rj   R   RZ   R[   R?   R   Re   RJ   RG   R4   R9   (    (    (    s$   /tmp/pip-build-kpPAdC/rsa/rsa/cli.pyRf   ç   s   	t   VerifyOperationc           B€ sP   e  Z d  Z d Z d Z d Z d Z d Z d Z e	 j
 Z d Z e Z d „  Z RS(	   s   Verify a signature.R-   s1   usage: %%prog [options] public_key signature_filess   Verifies a signature, exits with status 0 upon success, prints an error message and exits with status 1 upon error.t   verifyt   verifiedt	   Verifyingi   c         C€ s   | d } t  | d ƒ  } | j ƒ  } Wd QXy t j | | | ƒ Wn  t j k
 rg t d ƒ ‚ n Xt d d t j ƒd S(   s   Verifies files.i   RH   Ns   Verification failed.s   Verification OKR   (	   R   RI   R   Rn   t   VerificationErrorR   R   R   R   (   R5   R7   R'   R%   t   signature_filet   sigfileRh   (    (    s$   /tmp/pip-build-kpPAdC/rsa/rsa/cli.pyR9     s    
(   RS   RT   RU   RF   R   R   RZ   R[   R?   R   R]   RJ   RG   t   FalseRA   R9   (    (    (    s$   /tmp/pip-build-kpPAdC/rsa/rsa/cli.pyRm     s   	t   BigfileOperationc           B€ s;   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   s>   CryptoOperation that doesn't read the entire file into memory.c         C€ s   t  j |  ƒ g  |  _ d  S(   N(   R,   R6   t   file_objects(   R5   (    (    s$   /tmp/pip-build-kpPAdC/rsa/rsa/cli.pyR6   #  s    c         C€ s"   x |  j  D] } | j ƒ  q
 Wd S(   s   Closes any open file handles.N(   Rv   t   close(   R5   t   fobj(    (    s$   /tmp/pip-build-kpPAdC/rsa/rsa/cli.pyt   __del__(  s    c         C€ s…   |  j  ƒ  \ } } |  j | d | j ƒ } |  j | j ƒ } |  j | j ƒ } t |  j j	 ƒ  d t
 j ƒ|  j | | | | ƒ d S(   s   Runs the program.i    R   N(   R:   R;   R<   t
   get_infileR>   t   get_outfileRC   R   R?   R@   R   R   R9   (   R5   R$   R%   R8   RQ   R*   (    (    s$   /tmp/pip-build-kpPAdC/rsa/rsa/cli.pyRE   .  s    c         C€ s_   | r? t  d | d t j ƒt | d ƒ } |  j j | ƒ n t  d d t j ƒt j } | S(   s   Returns the input file objects   Reading input from %sR   RH   s   Reading input from stdin(   R   R   R   R   Rv   t   appendRO   (   R5   RP   Rx   (    (    s$   /tmp/pip-build-kpPAdC/rsa/rsa/cli.pyRz   =  s    	c         C€ s_   | r? t  d | d t j ƒt | d ƒ } |  j j | ƒ n t  d d t j ƒt j } | S(   s   Returns the output file objects   Will write output to %sR   R   s   Will write output to stdout(   R   R   R   R   Rv   R|   R"   (   R5   RR   Rx   (    (    s$   /tmp/pip-build-kpPAdC/rsa/rsa/cli.pyR{   J  s    	(   RS   RT   RU   R6   Ry   RE   Rz   R{   (    (    (    s$   /tmp/pip-build-kpPAdC/rsa/rsa/cli.pyRu      s   				t   EncryptBigfileOperationc           B€ s8   e  Z d  Z d Z d Z d Z d Z d Z d d „ Z	 RS(   s#   Encrypts a file to VARBLOCK format.R-   s’   Encrypts 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.R`   Ra   Rb   c         C€ s   t  j j | | | ƒ S(   s   Encrypts files to VARBLOCK.(   R   t   bigfilet   encrypt_bigfile(   R5   RQ   R*   R'   R%   (    (    s$   /tmp/pip-build-kpPAdC/rsa/rsa/cli.pyR9   c  s    N(
   RS   RT   RU   RF   R   RZ   R[   R?   RY   R9   (    (    (    s$   /tmp/pip-build-kpPAdC/rsa/rsa/cli.pyR}   X  s   t   DecryptBigfileOperationc           B€ sA   e  Z d  Z d Z d Z d Z d Z d Z e j	 Z
 d d „ Z RS(   s#   Decrypts a file in VARBLOCK format.Rd   sQ   Decrypts an encrypted VARBLOCK file that was encrypted with pyrsa-encrypt-bigfileR.   R/   R0   c         C€ s   t  j j | | | ƒ S(   s   Decrypts a VARBLOCK file.(   R   R~   t   decrypt_bigfile(   R5   RQ   R*   R(   R%   (    (    s$   /tmp/pip-build-kpPAdC/rsa/rsa/cli.pyR9   t  s    N(   RS   RT   RU   RF   R   RZ   R[   R?   R   Re   RJ   RY   R9   (    (    (    s$   /tmp/pip-build-kpPAdC/rsa/rsa/cli.pyR€   i  s   	(   RU   t
   __future__R    R   RV   R   t   optparseR   R   t   rsa.bigfilet	   rsa.pkcs1t   sortedt   pkcs1Rj   t   keysR+   t   objectR,   R_   Rc   Rf   Rm   Ru   R}   R€   R`   R.   Rg   Rn   R   R   (    (    (    s$   /tmp/pip-build-kpPAdC/rsa/rsa/cli.pyt   <module>   s.   	7j8					