o a&@sdZddlZddlZddlZddlZddlZddlZddlZeej j Z ej ej ejefZdddZGdddejdZGd d d eZGd d d eZGd ddeZGdddeZeZeZeZeZdS)zWCommandline scripts. These scripts are called by the executables defined in setup.py. Nreturnc Cstjddd}|jdddd|jdd dd d|jd d d dd|tjdd\}}t|dkr;|tdzt |d}Wn t yc}z|t d|dtj dtd|d}~wwt d|tj dt |\}}|jrt d|jtj d|j|jd}t|jd }||Wdn1swY|j|jd}|jrt d|jtj dt|jd}||WddS1swYdSt dtj dtjj|dS)zKey generator.zusage: %prog [options] keysizez/Generates a new RSA key pair 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.typehelp-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 PEMPEMDERr r choicesdefaultNrzNot a valid number: %sfilezGenerating %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 ValueErrorprintstderrrsanewkeyspubout save_pkcs1formopenwriteoutstdoutbuffer) parserclicli_argskeysizeexpub_keypriv_keydataoutfiler6)/usr/lib/python3/dist-packages/rsa/cli.pykeygen!sb    "r8c @seZdZdZdZdZdZdZdZdZ dZ d Z d Z d Z ejZd$ddZejdedejjded ejfddZd$ddZd ejejeje ffddZ!de de d ejjfddZ"de d efddZ#d ed!e d d fd"d#Z$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.rTrNcCs4|j|jj|_|j|jj|_|j|jj|_dS)N)r __class____dict__ input_help output_help)selfr6r6r7__init__wszCryptoOperation.__init__indatakeyr/cCsdS)zPerforms the program's operation. Implement in a subclass. :returns: the data to write to the output. Nr6)rCrErFr/r6r6r7perform_operation|sz!CryptoOperation.perform_operationcCsh|\}}||d|j}||j}t|jtj d| |||}|j r2| ||j dSdS)zRuns the program.rrN) parse_cliread_keykeyform read_infileinputr!operation_progressivetitlerr"rG has_output write_outfileoutput)rCr.r/rFrEoutdatar6r6r7__call__s  zCryptoOperation.__call__cCstj|j|jd}|jddd|jd|jr |jddd|jd|jdd |jd d d | t j d d\}}t ||j krG|td ||fS)zFParse the CLI options :returns: (cli_opts, cli_args) rz-iz--inputrrr z--outputz --keyformz&Key format of the %s key - default PEMr r rrN)rrrrrrArOrBkeynamerrrrexpected_cli_argsrr)rCr-r.r/r6r6r7rHszCryptoOperation.parse_clifilenamerJcCsXtd|j|ftjdt|d }|}Wdn1s wY|j||S)zReads a public or private key.zReading %s key from %srrbN)r!rTrr"r(read key_class load_pkcs1)rCrVrJkeyfilekeydatar6r6r7rIs   zCryptoOperation.read_keyinnamecCsb|r$td|tjdt|d }|WdS1swYtdtjdtjjS)zRead the input filezReading input from %srrWNzReading input from stdin)r!rr"r(rXstdinr,)rCr]infiler6r6r7rKs   zCryptoOperation.read_infilerRoutnamecCsp|r(td|tjdt|d}||WddS1s!wYdStdtjdtjj|dS)zWrite the output filezWriting output to %srrNzWriting output to stdout)r!rr"r(r)r+r,)rCrRr`r5r6r6r7rPs  "zCryptoOperation.write_outfilerN)%__name__ __module__ __qualname____doc__rTrr operationoperation_pastrMrArBrUrOr# PublicKeyrYrDabcabstractmethodbytesrF AbstractKey IndexabletypingAnyrGrSTuplerValuesListstrrHrIrKrPr6r6r6r7r9ds<    r9) metaclassc @sFeZdZdZdZdZdZdZdZ dde d e j j d e d e fd d ZdS)EncryptOperationzEncrypts a file.r:zWEncrypts a file. The file must be shorter than the key length in order to be encrypted.encrypt encrypted encryptingr6rEr2r/rcCt|tjjs Jt||S)zEncrypts files.) isinstancer#rFrhrv)rCrEr2r/r6r6r7rG z"EncryptOperation.perform_operationNr6)rbrcrdrerTrrfrgrMrkr#rFrlrmrGr6r6r6r7rus$ruc @sLeZdZdZdZdZdZdZdZe j Z dde d e j jd ed e fd d ZdS)DecryptOperationzDecrypts a file.privatezgDecrypts a file. The original file must be shorter than the key length in order to have been encrypted.r<r=r>r6rEr3r/rcCry)zDecrypts files.)rzr#rF PrivateKeyr<)rCrEr3r/r6r6r7rGr{z"DecryptOperation.perform_operationNr|)rbrcrdrerTrrfrgrMr#rrYrkrFrlrmrGr6r6r6r7r}s&r}c@s^eZdZdZdZdZddeZdZ dZ dZ e j Zd Zd Zd ed e jjd edefddZdS) 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 signatureSigningz\Name of the file to write the signature to. Written to stdout if this option is not present.rEr3r/rcCsBt|tjjs J|d}|tvrtddtt|||S)z Signs files.rz%Invalid hash method, choose one of %sr)rzr#rFr HASH_METHODSrjoinr)rCrEr3r/ hash_methodr6r6r7rGs zSignOperation.perform_operationN)rbrcrdrerTrrrrrfrgrMr#rrYrUrBrkrFrlrmrGr6r6r6r7rs0rc@sTeZdZdZdZdZdZdZdZdZ e j Z dZ d Zd ed e jjd ed dfddZdS)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.verifyverified VerifyingrFrEr2r/rNc Cst|tjjs J|d}t|d }|}Wdn1s!wYz t|||WntjyA}ztd|d}~wwt dt j ddS)zVerifies files.rrWNzVerification failed.zVerification OKr) rzr#rFrhr(rXrVerificationErrorrr!rr")rCrEr2r/signature_filesigfilerr1r6r6r7rG+s   z!VerifyOperation.perform_operation)rbrcrdrerTrrrfrgrMr#rhrYrUrOrkrFrlrmrGr6r6r6r7rs*rra)rerirrnrr#rsa.key rsa.pkcs1sortedpkcs1rkeysUnionrprrrsrmr8ABCMetar9rur}rrrvr<rrr6r6r6r7s( Cn!#