B d&@sdZddlZddlZddlZddlZddlZddlZddlZeej j Z ej ej ejefZddddZGdddejdZGd d d eZGd d d eZGd ddeZGdddeZeZeZeZeZdS)zWCommandline scripts. These scripts are called by the executables defined in setup.py. N)returnc Cstjddd}|jdddd|jdd dd d|jd d d dd|tjdd\}}t|dkrv|tdyt |d}Wn:t k r|t d|dtj dtdYnXt d|tj dt |\}}|jr.t d|jtj d|j|jd}t|jd}||WdQRX|j|jd}|jr|t d|jtj dt|jd}||WdQRXnt dtj dtjj|dS)zKey generator.zusage: %prog [options] keysizez.Generates a new RSA keypair of "keysize" bits.)usage descriptionz--puboutstringzOutput 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.)typehelpz-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)PEMDERr)rchoicesdefaultNrzNot a valid number: %s)filezGenerating %i-bit keyzWriting public key to %s)formatwbzWriting private key to %szWriting private key to stdout)optparse OptionParser add_option parse_argssysargvlen print_help SystemExitint ValueErrorprintstderrrsaZnewkeysZpuboutZ save_pkcs1formopenwriteoutstdoutbuffer)parserclicli_argsZkeysizepub_keypriv_keydataoutfiler+./tmp/pip-target-jj3kjtpb/lib/python/rsa/cli.pykeygen!sB   r-c@seZdZdZdZdZdZdZdZdZ dZ d Z d Z d Z ejZd d ddZejeejjeejdddZd d ddZejejeje fd ddZ!e e ejjdddZ"e edddZ#ee d dddZ$d S) CryptoOperationz9CLI callable that operates with input, output, and a key.publicz'usage: %%prog [options] %(keyname)s_keydecrypt 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 TN)rcCs4|j|jj|_|j|jj|_|j|jj|_dS)N)r __class____dict__ input_help output_help)selfr+r+r,__init__jszCryptoOperation.__init__)indatakeyr&rcCsdS)zPerforms the program's operation. Implement in a subclass. :returns: the data to write to the output. Nr+)r8r:r;r&r+r+r,perform_operationosz!CryptoOperation.perform_operationcCsd|\}}||d|j}||j}t|jtj d| |||}|j r`| ||j dS)zRuns the program.r)r N) parse_cliread_keykeyform read_infileinputroperation_progressivetitlerrr< has_output write_outfileoutput)r8r%r&r;r:outdatar+r+r,__call__ys  zCryptoOperation.__call__cCstj|j|jd}|jddd|jd|jr@|jddd|jd|jdd |jd d d | t j d d\}}t ||j kr|td ||fS)zFParse the CLI options :returns: (cli_opts, cli_args) )rrz-iz--inputr)rrz-oz--outputz --keyformz&Key format of the %s key - default PEM)rr r)rr r r N)rrrrrr6rDr7keynamerrrrexpected_cli_argsrr)r8r$r%r&r+r+r,r=s zCryptoOperation.parse_cli)filenamer?rc CsDtd|j|ftjdt|d}|}WdQRX|j||S)zReads a public or private key.zReading %s key from %s)r rbN)rrIrrrread key_classZ load_pkcs1)r8rKr?keyfileZkeydatar+r+r,r>s zCryptoOperation.read_key)innamerc CsJ|r0td|tjdt|d }|SQRXtdtjdtjjS)zRead the input filezReading input from %s)r rLNzReading input from stdin)rrrrrMstdinr#)r8rPinfiler+r+r,r@s  zCryptoOperation.read_infile)rGoutnamerc CsX|r8td|tjdt|d}||WdQRXntdtjdtjj|dS)zWrite the output filezWriting output to %s)r rNzWriting output to stdout)rrrrr r"r#)r8rGrSr*r+r+r,rEs  zCryptoOperation.write_outfile)%__name__ __module__ __qualname____doc__rIrr operationoperation_pastrBr6r7rJrDr PublicKeyrNr9abcabstractmethodbytesr; AbstractKey IndexabletypingAnyr<rHTuplerValuesListstrr=r>r@rEr+r+r+r,r.Xs*   r.) metaclassc@s>eZdZdZdZdZdZdZdZd e e j j e e dd d Zd S) EncryptOperationzEncrypts a file.r/zWEncrypts a file. The file must be shorter than the key length in order to be encrypted.encryptZ encryptedZ encryptingr+)r:r'r&rcCst|tjjstt||S)zEncrypts files.) isinstancerr;rZAssertionErrorrh)r8r:r'r&r+r+r,r<sz"EncryptOperation.perform_operationN)r+)rTrUrVrWrIrrXrYrBr]rr;r^r_r<r+r+r+r,rgsrgc@sDeZdZdZdZdZdZdZdZe j Z d e e j jee dd d Zd S) DecryptOperationzDecrypts a file.privatezgDecrypts a file. The original file must be shorter than the key length in order to have been encrypted.r1r2r3r+)r:r(r&rcCst|tjjstt||S)zDecrypts files.)rirr; PrivateKeyrjr1)r8r:r(r&r+r+r,r<sz"DecryptOperation.perform_operationN)r+)rTrUrVrWrIrrXrYrBrrmrNr]r;r^r_r<r+r+r+r,rksrkc@sXeZdZdZdZdZddeZdZ dZ dZ e j Zd Zd Zee jjeed d d ZdS) SignOperationz Signs a file.rlz/usage: %%prog [options] private_key hash_methodzCSigns a file, outputs the signature. Choose the hash method from %sz, sign signatureZSigningz\Name of the file to write the signature to. Written to stdout if this option is not present.)r:r(r&rcCsBt|tjjst|d}|tkr4tddtt|||S)z Signs files.r z%Invalid hash method, choose one of %sz, ) rirr;rmrj HASH_METHODSrjoinro)r8r:r(r&Z hash_methodr+r+r,r<s zSignOperation.perform_operationN)rTrUrVrWrIrrsrrrrXrYrBrrmrNrJr7r]r;r^r_r<r+r+r+r,rns rnc@sNeZdZdZdZdZdZdZdZdZ e j Z dZ d Zee jjed d d d Zd S)VerifyOperationzVerify 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.verifyZverifiedZ VerifyingrqFN)r:r'r&rc Cs|t|tjjst|d}t|d}|}WdQRXyt|||Wntjk rht dYnXt dt j ddS)zVerifies files.r rLNzVerification failed.zVerification OK)r ) rirr;rZrjrrMruZVerificationErrorrrrr)r8r:r'r&Zsignature_fileZsigfilerpr+r+r,r<s z!VerifyOperation.perform_operation)rTrUrVrWrIrrrXrYrBrrZrNrJrDr]r;r^r_r<r+r+r+r,rtsrt)rWr[rr`rrZrsa.keyZ rsa.pkcs1sortedZpkcs1rrkeysUnionrbrdrer_r-ABCMetar.rgrkrnrtrhr1rorur+r+r+r,s&7i