ó |~úVc@sædZdZdZdZddlZddlZddlZddlZddlZddl m Z ddl Z e j de ƒe j deƒd „Zd „Zd „Zd „Zd „Zd„Zd„Zd„Zd„Zd„Zd„Zd„Zd„Zd„Zd„Zd„Zd„Zd„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,d'„Z-e d(krÍddl.Z.e.j/ƒnd)d*d+d,d-gZ0dS(.sDeprecated version of the RSA module .. deprecated:: 3.0 This submodule is deprecated and will be completely removed as of version 4.0. s;Sybren Stuvel, Marloes de Boer, Ivo Tamboer, and Barry Meads 2010-02-08s2.0iÿÿÿÿN(tbytes4Insecure version of the RSA module is imported as %ssNThis submodule is deprecated and will be completely removed as of version 4.0.cCsttjtj|dƒƒƒS(sBReturns the number of bits required to hold a specific long numberi(tinttmathtceiltlog(tnumber((s:/opt/awscli/lib/python2.7/site-packages/rsa/_version200.pytbit_size+scCsDx=|dkr?||kr+||}}n|||}}qW|S(sOReturns the greatest common divisor of p and q >>> gcd(48, 180) 12 i((tptq((s:/opt/awscli/lib/python2.7/site-packages/rsa/_version200.pytgcd0s  cCsŒt|ƒtjkp't|ƒtjks9tdƒ‚nd}xF|D]>}|d9}t|ƒtjkrzt|ƒ}n||7}qFW|S(s7Converts a list of bytes or a string to an integer s You must pass a string or a listii(ttypettypestListTypet StringTypet TypeErrortord(tbytestintegerR((s:/opt/awscli/lib/python2.7/site-packages/rsa/_version200.pyt bytes2int<s*  cCszt|ƒtjkp't|ƒtjks9tdƒ‚nd}x4|dkrudt|d@ƒ|f}|d:}qBW|S(s0 Converts a number to a string of bytes sYou must pass a long or an inttis%s%siÿi(R R tLongTypetIntTypeRR(Rtstring((s:/opt/awscli/lib/python2.7/site-packages/rsa/_version200.pyt int2bytesLs*cCs÷t|ƒtjkp't|ƒtjks9tdƒ‚nd|koPdknrct|dƒSd|kozdknrt|dƒSd|ko¤d knr·t|d ƒS|d krÍtd ƒS|d krãtd ƒStd|ƒ‚dS(s‚Converts a number in the range of 0 to 63 into base 64 digit character in the range of '0'-'9', 'A'-'Z', 'a'-'z','-','_'. sYou must pass a long or an intii i0i i#i7i$i=i>i-i?i_sInvalid Base64 value: %iN(R R RRRRt ValueError(R((s:/opt/awscli/lib/python2.7/site-packages/rsa/_version200.pytto64\s*    cCsÙt|ƒtjkp't|ƒtjks9tdƒ‚nd|koPdknr]|dSd|kotdknr|dSd|ko˜dknr¥|d S|d krµd S|d krÅd Std|ƒ‚dS(snConverts an ordinal character value in the range of 0-9,A-Z,a-z,-,_ to a number in the range of 0-63. sYou must pass a long or an inti0i9iAiZi7iaizi=i-i>i_i?sInvalid Base64 value: %iN(R R RRRR(R((s:/opt/awscli/lib/python2.7/site-packages/rsa/_version200.pytfrom64vs*  cCszt|ƒtjkp't|ƒtjks9tdƒ‚nd}x4|dkrudt|d@ƒ|f}|d:}qBW|S(ssConverts a number to a string of base64 encoded characters in the range of '0'-'9','A'-'Z,'a'-'z','-','_'. sYou must pass a long or an intRis%s%si?i@(R R RRRR(RR((s:/opt/awscli/lib/python2.7/site-packages/rsa/_version200.pyt int2str64s*cCs’t|ƒtjkp't|ƒtjks9tdƒ‚nd}xL|D]D}|d9}t|ƒtjkrzt|ƒ}n|t|ƒ7}qFW|S(sƒConverts a base64 encoded string into an integer. The chars of this string in in the range '0'-'9','A'-'Z','a'-'z','-','_' s You must pass a string or a listii@(R R R R RRR(RRR((s:/opt/awscli/lib/python2.7/site-packages/rsa/_version200.pyt str642int¡s*  cCs2ttj|dƒƒ}tj|ƒ}t|ƒS(sPReads a random integer of approximately nbits bits rounded up to whole bytesg @(RRRtosturandomR(tnbitstnbytest randomdata((s:/opt/awscli/lib/python2.7/site-packages/rsa/_version200.pytread_random_int±scCscd}||d}t|ƒdd}t|d|dƒ}tj||ƒ}t|ƒ||S(s9Returns a random integer x with minvalue <= x <= maxvaluei iiii(RtmaxtrandomtrandintR"(tminvaluetmaxvaluet min_nbitstranget rangebytest rangebitsR((s:/opt/awscli/lib/python2.7/site-packages/rsa/_version200.pyR%¹s cCs©|dkrdSd}x||dkr”|d@rg|d|dd?d@rS| }n|||}}q||dd?d@r‡| }n|dL}qW|dkr¥dS|S(soCalculates the value of the Jacobi symbol (a/b) where both a and b are positive integers, and b is odd iiii((tatbtresult((s:/opt/awscli/lib/python2.7/site-packages/rsa/_version200.pytjacobiÎs     cCsAt||ƒ|}t||dd|ƒ}||kr=tStS(sUReturns False if n is an Euler pseudo-prime with base x, and True otherwise. ii(R/tpowtFalsetTrue(txtntjtf((s:/opt/awscli/lib/python2.7/site-packages/rsa/_version200.pytjacobi_witnessás  cCsAx:t|ƒD],}td|dƒ}t||ƒr tSq WtS(sÓCalculates whether n is composite (which is always correct) or prime (which is incorrect with error probability 2**-k) Returns False if the number is composite, and True if it's probably prime. i(R)R%R7R1R2(R4tktiR3((s:/opt/awscli/lib/python2.7/site-packages/rsa/_version200.pytrandomized_primality_testingìs cCst|dƒrtStS(s>Returns True if the number is prime, and False otherwise. i(R:R2R1(R((s:/opt/awscli/lib/python2.7/site-packages/rsa/_version200.pytis_primeüscCs7x0tr2t|ƒ}|dO}t|ƒrPqqW|S(sxReturns a prime number of max. 'math.ceil(nbits/8)*8' bits. In other words: nbits is rounded up to whole bytes. i(R2R"R;(RR((s:/opt/awscli/lib/python2.7/site-packages/rsa/_version200.pytgetprimes     cCst||ƒ}|dkS(s¤Returns True if a and b are relatively prime, and False if they are not. >>> are_relatively_prime(2, 3) 1 >>> are_relatively_prime(2, 4) 0 i(R (R,R-td((s:/opt/awscli/lib/python2.7/site-packages/rsa/_version200.pytare_relatively_primes cCs[||d}||d}t|ƒ}x&trPt|ƒ}||ks+Pq+q+W||fS(s5Returns a tuple of two different primes of nbits bitsi(R<R2(RtpbitstqbitsRR((s:/opt/awscli/lib/python2.7/site-packages/rsa/_version200.pytfind_p_q's    c CsÁd}d}d}d}|}|}x[|dkrt||ƒ}|||}}||||}}||||}}q'W|dkr›||7}n|dkr´||7}n|||fS(s@Returns a tuple (r, i, j) such that r = gcd(a, b) = ia + jb ii(tlong( R,R-R3tytlxtlytoatobR((s:/opt/awscli/lib/python2.7/site-packages/rsa/_version200.pyt extended_gcd2s   c Csö||}|d|d}xEtrctdt|dƒƒ}t||ƒrt||ƒrPqqWt||ƒ\}}}|dks¡td||fƒ‚n|dkr¼tdƒ‚n|||dksìtd|||fƒ‚n||fS(saCalculates an encryption and a decryption key for p and q, and returns them as a tuple (e, d)iiis.e (%d) and phi_n (%d) are not relatively primeis1New extended_gcd shouldn't return negative valuess6e (%d) and i (%d) are not mult. inv. modulo phi_n (%d)(R2R#R<R>RHt Exception( RRRR4tphi_nteR=R9R5((s:/opt/awscli/lib/python2.7/site-packages/rsa/_version200.pytcalculate_keysIs    cCs:t|ƒ\}}t|||ƒ\}}||||fS(s|Generate RSA keys of nbits bits. Returns (p, q, e, d). Note: this can take a long time, depending on the key size. (RARL(RRRRKR=((s:/opt/awscli/lib/python2.7/site-packages/rsa/_version200.pytgen_keysbscCsXtd|ƒ}t|ƒ\}}}}i|d6||d6i|d6|d6|d6fS(sÌGenerates public and private keys, and returns them as (pub, priv). The public key consists of a dict {e: ..., , n: ....). The private key consists of a dict {d: ...., p: ...., q: ....). i RKR4R=RR(R#RM(RRRRKR=((s:/opt/awscli/lib/python2.7/site-packages/rsa/_version200.pytnewkeysmscCst|ƒtjkr$t|ƒ}nt|ƒtjk rHtdƒ‚n|dks`||krotdƒ‚nt|ƒd}|d|>7}t|||ƒS(s@Encrypts a message using encryption key 'ekey', working modulo nsYou must pass a long or intisThe message is too longii( R R RRBRRt OverflowErrorRR0(tmessagetekeyR4tsafebit((s:/opt/awscli/lib/python2.7/site-packages/rsa/_version200.pyt encrypt_intyscCs4t|||ƒ}t|ƒd}|d|>8}|S(sLDecrypts a cypher text using the decryption key 'dkey', working modulo nii(R0R(t cyphertexttdkeyR4RPRR((s:/opt/awscli/lib/python2.7/site-packages/rsa/_version200.pyt decrypt_int‹scCs=g}x!|D]}|jt|ƒƒq Wdj|ƒ}|S(sAbase64encodes chops and combines them into a ',' delimited stringt,(tappendRtjoin(tchopstchipstvaluetencoded((s:/opt/awscli/lib/python2.7/site-packages/rsa/_version200.pyt encode64chops–s  cCs=|jdƒ}g}x!|D]}|jt|ƒƒqW|S(s9base64decodes and makes a ',' delimited string into chopsRW(tsplitRXR(RR[RZ((s:/opt/awscli/lib/python2.7/site-packages/rsa/_version200.pyt decode64chops£s  cCs¾t|ƒ}|d}t|ƒd}|d}||}||dkrW|d7}ng} xTt|ƒD]F} | |} || | |!} t| ƒ} | j|| ||ƒƒqjWt| ƒS(sÅChops the 'message' into integers that fit into n, leaving room for a safebit to be added to ensure that all messages fold during exponentiation. The MSB of the number n is not independant modulo n (setting it could cause overflow), so use the next lower bit for the safebit. Therefore reserve 2-bits in the number n for non-data bits. Calls specified encryption function for each chop. Used by 'encrypt' and 'sign'. iiii(tlenRR)RRXR^(RPtkeyR4tfuncreftmsglentmbitsRR tblockstcyphertbindextoffsettblockR\((s:/opt/awscli/lib/python2.7/site-packages/rsa/_version200.pyt chopstring¯s       cCsId}t|ƒ}x0|D](}||||ƒ}|t|ƒ7}qW|S(s…Glues chops back together into a string. calls funcref(integer, key, n) for each chop. Used by 'decrypt' and 'verify'. R(R`R(RRbR4RcRPRZtcparttmpart((s:/opt/awscli/lib/python2.7/site-packages/rsa/_version200.pyt gluechopsÏs   cCs6d|krtdƒ‚nt||d|dtƒS(s5Encrypts a string 'message' with the public key 'key'R4s(You must use the public key with encryptRK(RIRkRS(RPRb((s:/opt/awscli/lib/python2.7/site-packages/rsa/_version200.pytencryptßs cCs>d|krtdƒ‚nt||d|d|dtƒS(s3Signs a string 'message' with the private key 'key'Rs&You must use the private key with signR=R(RIRkRS(RPRb((s:/opt/awscli/lib/python2.7/site-packages/rsa/_version200.pytsignæs cCs>d|krtdƒ‚nt||d|d|dtƒS(s5Decrypts a string 'cypher' with the private key 'key'Rs)You must use the private key with decryptR=R(RIRnRV(RgRb((s:/opt/awscli/lib/python2.7/site-packages/rsa/_version200.pytdecryptís cCs6d|krtdƒ‚nt||d|dtƒS(s4Verifies a string 'cypher' with the public key 'key'R4s'You must use the public key with verifyRK(RIRnRV(RgRb((s:/opt/awscli/lib/python2.7/site-packages/rsa/_version200.pytverifyôs t__main__RNRoRqRpRr(1t__doc__t __author__t__date__t __version__RRR$tsysR t rsa._compatRtwarningstwarnt__name__tDeprecationWarningRR RRRRRRR"R%R/R7R:R;R<R>RARHRLRMRNRSRVR^R`RkRnRoRpRqRrtdoctestttestmodt__all__(((s:/opt/awscli/lib/python2.7/site-packages/rsa/_version200.pyts`