B Ðñ¢dó&ã@sàdZddlZddlZddlZddlZddlZddlZddlZeej j   ¡ƒZ ej ej ejefZddœdd„ZGdd„dejd�ZGd d „d eƒZGd d „d eƒZGd d„deƒZGdd„deƒZeƒZeƒZeƒZeƒZdS)zWCommandline scripts. These scripts are called by the executables defined in setup.py. éN)Úreturnc Csœtjddd�}|jdddd�|jdd dd d�|jd d d dd�| tjdd…¡\}}t|ƒdkrv| ¡tdƒ‚yt |dƒ}Wn:t k rÀ| ¡t d|dtj d�tdƒ‚YnXt d|tj d�t  |¡\}}|j�r.t d|jtj d�|j|jd�}t|jdƒ�}| |¡WdQRX|j|jd�}|j�r|t d|jtj d�t|jdƒ�}| |¡WdQRXnt dtj d�tjj |¡dS)zKey generator.zusage: %prog [options] keysizez.Generates a new RSA keypair 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Úhelpz-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éNrzNot a valid number: %s)ÚfilezGenerating %i-bit keyzWriting public key to %s)ÚformatÚwbzWriting private key to %szWriting private key to stdout)ÚoptparseÚ OptionParserÚ add_optionÚ parse_argsÚsysÚargvÚlenÚ print_helpÚ SystemExitÚintÚ ValueErrorÚprintÚstderrÚrsaZnewkeysZpuboutZ save_pkcs1ÚformÚopenÚwriteÚoutÚstdoutÚbuffer)ÚparserÚcliÚcli_argsZkeysizeÚpub_keyÚpriv_keyÚdataÚoutfile©r+ú./tmp/pip-target-jj3kjtpb/lib/python/rsa/cli.pyÚkeygen!sB   r-c@sÒeZdZdZdZdZdZdZdZdZ dZ d Z d Z d Z ejZd d œdd„Zejeejjeejdœdd„ƒZd d œdd„Zejejeje fd œdd„Z!e e ejjdœdd„Z"e edœdd„Z#ee d dœdd„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 TN)rcCs4|j|jj|_|j|jj|_|j|jj|_dS)N)rÚ __class__Ú__dict__Ú input_helpÚ output_help)Úselfr+r+r,Ú__init__jszCryptoOperation.__init__)ÚindataÚkeyr&rcCsdS)z€Performs 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|j ¡tj d�|  |||¡}|j r`|  ||j ¡dS)zRuns the program.r)r N)Ú parse_cliÚread_keyÚkeyformÚ read_infileÚinputrÚoperation_progressiveÚtitlerrr<Ú has_outputÚ write_outfileÚoutput)r8r%r&r;r:Úoutdatar+r+r,Ú__call__ys  zCryptoOperation.__call__cCs–tj|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)rrrrrr6rDr7ÚkeynamerrrrÚexpected_cli_argsrr)r8r$r%r&r+r+r,r=ˆs zCryptoOperation.parse_cli)Úfilenamer?rc CsDtd|j|ftjd�t|dƒ�}| ¡}WdQRX|j ||¡S)zReads a public or private key.zReading %s key from %s)r ÚrbN)rrIrrrÚreadÚ key_classZ load_pkcs1)r8rKr?ÚkeyfileZkeydatar+r+r,r>¡s zCryptoOperation.read_key)Úinnamerc CsJ|r0td|tjd�t|dƒ� }| ¡SQRXtdtjd�tjj ¡S)zRead the input filezReading input from %s)r rLNzReading input from stdin)rrrrrMÚstdinr#)r8rPÚinfiler+r+r,r@ªs  zCryptoOperation.read_infile)rGÚoutnamerc CsX|r8td|tjd�t|dƒ�}| |¡WdQRXntdtjd�tjj |¡dS)zWrite the output filezWriting output to %s)r rNzWriting output to stdout)rrrrr r"r#)r8rGrSr*r+r+r,rEµs  zCryptoOperation.write_outfile)%Ú__name__Ú __module__Ú __qualname__Ú__doc__rIrrÚ operationÚoperation_pastrBr6r7rJrDrÚ PublicKeyrNr9ÚabcÚabstractmethodÚbytesr;Ú AbstractKeyÚ IndexableÚtypingÚAnyr<rHÚTuplerÚValuesÚListÚstrr=r>r@rEr+r+r+r,r.Xs*   r.)Ú metaclassc@s>eZdZdZdZdZdZdZdZd e e j j e e dœd 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|tjjƒst‚t ||¡S)zEncrypts files.)Ú isinstancerr;rZÚAssertionErrorrh)r8r:r'r&r+r+r,r<Ësz"EncryptOperation.perform_operationN)r+)rTrUrVrWrIrrXrYrBr]rr;r^r_r<r+r+r+r,rgÁsrgc@sDeZdZdZdZdZdZdZdZe j Z d e e j jee dœd 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|tjjƒst‚t ||¡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,rkÒsrkc@sXeZdZdZdZdZdd e¡ZdZ 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Ú signatureZSigningéz\Name of the file to write the signature to. Written to stdout if this option is not present.)r:r(r&rcCsBt|tjjƒst‚|d}|tkr4tdd t¡ƒ‚t |||¡S)z Signs files.r z%Invalid hash method, choose one of %sz, ) rirr;rmrjÚ HASH_METHODSrÚjoinro)r8r:r(r&Z hash_methodr+r+r,r<ôs zSignOperation.perform_operationN)rTrUrVrWrIrrsrrrrXrYrBrrmrNrJr7r]r;r^r_r<r+r+r+r,rnäs 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|tjjƒst‚|d}t|dƒ�}| ¡}WdQRXyt |||¡Wntjk rht dƒ‚YnXt dt j d�dS)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.pkcs1ÚsortedZpkcs1rrÚkeysÚUnionrbrdrer_r-ÚABCMetar.rgrkrnrtrhr1rorur+r+r+r,Ús&7i