5Xc@`sdZddlmZmZmZddlmZmZmZ m Z ddl m Z mZmZddlZejeZddlmZddlmZmZmZmZmZmZmZmZm Z m!Z!m"Z"m#Z#ddl$m%Z%d d d d d ddddddddddddddddgZ&e!dZ'e!dZ(e!d Z)e!d!Z*e'e!d"Z+e!d#Z,e!d$Z-e!d%Z.eed&Z/d'Z0d(Z1d"Z2e3ee/Z4e5d)Z6d*Z7d+Z8d,Z9d"Z:d-Z;d.Z<d/Z=d0Z e6id1d26d3d46Z>e2d5Z?d6Zde@fd7YZAdeAfd8YZBeBe)ZCeBe)d9eDZEeBe*d9eDZFdS(:sC passlib.utils.binary - binary data encoding/decoding/manipulation i(tabsolute_importtdivisiontprint_function(t b64encodet b64decodet b32decodet b32encode(t b2a_base64t a2b_base64tErrorN(texc( tPY3t bascii_to_strtirangetimaptiter_byte_charstjoin_byte_valuestjoin_byte_elemst nextgettertsuppress_causetutunicodetunicode_or_bytes_types(tmemoized_propertyt BASE64_CHARStPADDED_BASE64_CHARSt AB64_CHARSt HASH64_CHARSt BCRYPT_CHARSt HEX_CHARStLOWER_HEX_CHARStUPPER_HEX_CHARStALL_BYTE_VALUEStcompile_byte_translationt ab64_encodet ab64_decodet b64s_encodet b64s_decodeRRt Base64EnginetLazyBase64Engineth64th64bigtbcrypt64s@ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/s@ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789./s@./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzs@./ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789t=t0123456789abcdefABCDEFt0123456789ABCDEFt0123456789abcdefittcC`s|dkrt}ntt|}xc|jD]U\}}t|tr_t|}nt|tr|j d}n||| output byte (str or int). :param source: optional existing byte translation string to use as base. (must be 255-length byte string). defaults to identity mapping. :returns: 255-length byte string for passing to bytes().translate. tasciiN( tNonet_TRANSLATE_SOURCEtlistRtitemst isinstanceRtordRtencodetB_EMPTYtjoin(tmappingtsourcettargettktv((s8/usr/lib/python2.7/site-packages/passlib/utils/binary.pyR!ns  cC`st|jtS(sr encode using shortened base64 format which omits padding & whitespace. uses default ``+/`` altchars. (Rtrstript _BASE64_STRIP(tdata((s8/usr/lib/python2.7/site-packages/passlib/utils/binary.pyR$scC`st|trKy|jd}WqKtk rGttdqKXnt|d@}|dkrjn>|dkr|t7}n%|dkr|t7}n tdyt |SWn%t k r}tt |nXdS(sq decode from shortened base64 format which omits padding & whitespace. uses default ``+/`` altchars. R1s4string argument should contain only ASCII charactersiiisinvalid base64 inputN( R6RR8tUnicodeEncodeErrorRt ValueErrortlent _BASE64_PAD2t _BASE64_PAD1Rt_BinAsciiErrort TypeError(RBtoffterr((s8/usr/lib/python2.7/site-packages/passlib/utils/binary.pyR%s"       s= s==cC`st|jddS(s encode using shortened base64 format which omits padding & whitespace. uses custom ``./`` altchars. it is primarily used by Passlib's custom pbkdf2 hashes. t+t.(R$treplace(RB((s8/usr/lib/python2.7/site-packages/passlib/utils/binary.pyR"scC`sat|trKy|jd}WqKtk rGttdqKXnt|jddS(s decode from shortened base64 format which omits padding & whitespace. uses custom ``./`` altchars, but supports decoding normal ``+/`` altchars as well. it is primarily used by Passlib's custom pbkdf2 hashes. R1s4string argument should contain only ASCII charactersRMRL(R6RR8RCRRDR%RN(RB((s8/usr/lib/python2.7/site-packages/passlib/utils/binary.pyR#s  cC`stt|jtS(sh wrapper around :func:`base64.b32encode` which strips padding, and returns a native string. (R t _b32encodeR@tB_EQUAL(R<((s8/usr/lib/python2.7/site-packages/passlib/utils/binary.pyRstBt8tOt0icC`set|tr!|jd}n|jt}t|d@}|rX|t| 7}nt|tS(s wrapper around :func:`base64.b32decode` which handles common mistyped chars. padding optional, ignored if present. R1i( R6RR8t translatet_b32_translateREt_b32_decode_padt _b32decodetTrue(R<t remainder((s8/usr/lib/python2.7/site-packages/passlib/utils/binary.pyRscB`s:eZdZdZdZdZdZdZdZ e dZ e dZ dZdZdZdZdZdZd Zed Zed Zd Zd ZdZdZdZdZdZdZdZ dZ!dZ"dZ#dZ$dZ%dZ&dZ'RS(scProvides routines for encoding/decoding base64 data using arbitrary character mappings, selectable endianness, etc. :arg charmap: A string of 64 unique characters, which will be used to encode successive 6-bit chunks of data. A character's position within the string should correspond to its 6-bit value. :param big: Whether the encoding should be big-endian (default False). .. note:: This class does not currently handle base64's padding characters in any way what so ever. Raw Bytes <-> Encoded Bytes =========================== The following methods convert between raw bytes, and strings encoded using the engine's specific base64 variant: .. automethod:: encode_bytes .. automethod:: decode_bytes .. automethod:: encode_transposed_bytes .. automethod:: decode_transposed_bytes .. .. automethod:: check_repair_unused .. automethod:: repair_unused Integers <-> Encoded Bytes ========================== The following methods allow encoding and decoding unsigned integers to and from the engine's specific base64 variant. Endianess is determined by the engine's ``big`` constructor keyword. .. automethod:: encode_int6 .. automethod:: decode_int6 .. automethod:: encode_int12 .. automethod:: decode_int12 .. automethod:: encode_int24 .. automethod:: decode_int24 .. automethod:: encode_int64 .. automethod:: decode_int64 Informational Attributes ======================== .. attribute:: charmap unicode string containing list of characters used in encoding; position in string matches 6bit value of character. .. attribute:: bytemap bytes version of :attr:`charmap` .. attribute:: big boolean flag indicating this using big-endian encoding. cC`st|tr!|jd}n$t|tsEtj|dnt|dkrftdntt|dkrtdn||_ |j |_ t dt |D}|j |_||_|r|j|_|j|_n|j|_|j|_dS(Nslatin-1tcharmapi@s'charmap must be 64 characters in lengths-charmap must not contain duplicate characterscs`s!|]\}}||fVqdS(N((t.0tidxtvalue((s8/usr/lib/python2.7/site-packages/passlib/utils/binary.pys ]s(R6RR8tbytesR tExpectedStringErrorRERDtsettbytemapt __getitem__t _encode64tdictt enumeratet _decode64tbigt_encode_bytes_bigt _encode_bytest_decode_bytes_bigt _decode_bytest_encode_bytes_littlet_decode_bytes_little(tselfR[Rhtlookup((s8/usr/lib/python2.7/site-packages/passlib/utils/binary.pyt__init__Qs$      cC`s|jjdS(scharmap as unicodeslatin-1(Rbtdecode(Ro((s8/usr/lib/python2.7/site-packages/passlib/utils/binary.pyR[sscC`st|ts+tdt|fntt|d\}}tratt|}ntd|D}|j |||}t t |j |}|S(sencode bytes to base64 string. :arg source: byte string to encode. :returns: byte string containing encoded data. ssource must be bytes, not %sics`s|]}t|VqdS(N(R7(R\telem((s8/usr/lib/python2.7/site-packages/passlib/utils/binary.pys s( R6R_RIttypetdivmodRER RtiterRjRRRd(RoR<tchunksttailt next_valuetgentout((s8/usr/lib/python2.7/site-packages/passlib/utils/binary.pyt encode_bytes{scc`sd}xq||kry|}|}|}|d@V|d@d>|d?BV|d@d>|d?BV|d?V|d7}q W|r|}|dkr|d@V|d?Vq|}|d@V|d@d>|d?BV|d?Vnd S( s>helper used by encode_bytes() to handle little-endian encodingii?iiiiiiN((RoRyRwRxR]tv1tv2tv3((s8/usr/lib/python2.7/site-packages/passlib/utils/binary.pyRms&           cc`sd}xq||kry|}|}|}|d?V|d@d>|d?BV|d@d>|d?BV|d@V|d7}q W|r|}|dkr|d?V|d@d>Vq|}|d?V|d@d>|d?BV|d@d>Vnd S( s;helper used by encode_bytes() to handle big-endian encodingiiiiiii?iN((RoRyRwRxR]R}R~R((s8/usr/lib/python2.7/site-packages/passlib/utils/binary.pyRis&          cC`st|ts+tdt|fntt|d\}}|dkratdntt|j |}yt |j |||SWn-t k r}td|j dfnXdS(sdecode bytes from base64 string. :arg source: byte string to decode. :returns: byte string containing decoded data. ssource must be bytes, not %siis(input string length cannot be == 1 mod 4sinvalid character: %riN(R6R_RIRtRuRERDRRRgRRltKeyErrortargs(RoR<RwRxRyRK((s8/usr/lib/python2.7/site-packages/passlib/utils/binary.pyt decode_bytess c c`sd}xu||kr}|}|}|}|}||d@d>BV|d?|d@d>BV|d?|d>BV|d7}q W|r|}|}||d@d>BV|dkr|}|d?|d@d>BVqndS( s>helper used by decode_bytes() to handle little-endian encodingiiiiiiiN(( RoRyRwRxR]R}R~Rtv4((s8/usr/lib/python2.7/site-packages/passlib/utils/binary.pyRns"        c c`sd}xu||kr}|}|}|}|}|d>|d?BV|d@d>|d?BV|d@d>|BV|d7}q W|r|}|}|d>|d?BV|dkr|}|d@d>|d?BVqndS( s;helper used by decode_bytes() to handle big-endian encodingiiiiiiiN(( RoRyRwRxR]R}R~RR((s8/usr/lib/python2.7/site-packages/passlib/utils/binary.pyRks"        c`sUtfdt|jD}|jfdt|jDt|S(s2helper to generate set of valid last chars & bytesc3`s%|]\}}|@s|VqdS(N((R\titc(tbits(s8/usr/lib/python2.7/site-packages/passlib/utils/binary.pys Gsc3`s%|]\}}|@s|VqdS(N((R\RR(R(s8/usr/lib/python2.7/site-packages/passlib/utils/binary.pys Hs(RaRfRbtupdateR[t frozenset(RoRtpset((Rs8/usr/lib/python2.7/site-packages/passlib/utils/binary.pyt __make_padsetEs%&cC`s)|jrdnd}||j|fS(sDmask to clear padding bits, and valid last bytes (for strings 2 % 4)iii<(Rht_Base64Engine__make_padset(RoR((s8/usr/lib/python2.7/site-packages/passlib/utils/binary.pyt _padinfo2KscC`s)|jrdnd}||j|fS(sDmask to clear padding bits, and valid last bytes (for strings 3 % 4)iii0(RhR(RoR((s8/usr/lib/python2.7/site-packages/passlib/utils/binary.pyt _padinfo3RscC`st|d@}|dkr.|j\}}n:|dkrL|j\}}n|s\t|fStd|d}||krt|fSt|tr|j}||j||@}n4|j |j ||@}t rt |g}nt |d |fS(shelper to detect & clear invalid unused bits in last character. :arg source: encoded data (as ascii bytes or unicode). :returns: `(True, result)` if the string was repaired, `(False, source)` if the string was ok as-is. iissource length must != 1 mod 4i(RERRtFalseRDR6RR[tindexRdRgR R_RY(RoR<Rxtmasktpadsettlasttcm((s8/usr/lib/python2.7/site-packages/passlib/utils/binary.pytcheck_repair_unusedYs&         cC`s|j|dS(Ni(R(RoR<((s8/usr/lib/python2.7/site-packages/passlib/utils/binary.pyt repair_unusedsc`sTtts+tdtfntfd|D}|j|S(s>encode byte string, first transposing source using offset listssource must be bytes, not %sc3`s|]}|VqdS(N((R\RJ(R<(s8/usr/lib/python2.7/site-packages/passlib/utils/binary.pys s(R6R_RIRtRR|(RoR<toffsetsttmp((R<s8/usr/lib/python2.7/site-packages/passlib/utils/binary.pytencode_transposed_bytesscC`sV|j|}dgt|}x't||D]\}}|||||}qWWn$t k rtd|fnX|r|r||L}q|d|>dM}n|S(sdecode base64 string -> integer :arg source: base64 string to decode. :arg bits: number of bits in resulting integer. :raises ValueError: * if the string contains invalid base64 characters. * if the string is not long enough - it must be at least ``int(ceil(bits/6))`` in length. :returns: a integer in the range ``0 <= n < 2**bits`` ssource must be bytes, not %sissource must be %d charsisinvalid character in string: %ri( R6R_RIRtRhRERDRgtreversedR( RoR<RRhtpadtcharsRrR{R((s8/usr/lib/python2.7/site-packages/passlib/utils/binary.pyt _decode_ints&     cC`st|ts+tdt|fnt|dkrLtdntr_|d}ny|j|SWntk rtdnXdS(s(decode single character -> 6 bit integerssource must be bytes, not %sissource must be exactly 1 byteisinvalid characterN( R6R_RIRtRERDR RgR(RoR<((s8/usr/lib/python2.7/site-packages/passlib/utils/binary.pyt decode_int6s  cC`st|ts+tdt|fnt|dkrLtdn|j}yM|jr||d||dd>S||d||dd>SWntk rtdnXdS( s'decodes 2 char string -> 12-bit integerssource must be bytes, not %sissource must be exactly 2 bytesiiisinvalid characterN( R6R_RIRtRERDRgRhR(RoR<Rr((s8/usr/lib/python2.7/site-packages/passlib/utils/binary.pyt decode_int12s   $ cC`st|ts+tdt|fnt|dkrLtdn|j}y|jr||d||dd>||dd>||d d >S||d ||dd>||dd>||dd >SWntk r td nXd S( s'decodes 4 char string -> 24-bit integerssource must be bytes, not %sissource must be exactly 4 bytesiiiii iisinvalid characterN( R6R_RIRtRERDRgRhR(RoR<Rr((s8/usr/lib/python2.7/site-packages/passlib/utils/binary.pyt decode_int24s  DH cC`s|j|dS(s&decode 5 char string -> 30 bit integeri(R(RoR<((s8/usr/lib/python2.7/site-packages/passlib/utils/binary.pyt decode_int30scC`s|j|dS(sdecode 11 char base64 string -> 64-bit integer this format is used primarily by des-crypt & variants to encode the DES output value used as a checksum. i@(R(RoR<((s8/usr/lib/python2.7/site-packages/passlib/utils/binary.pyt decode_int64sc`sy| d}||7}|jrAt|ddd}|Kntd|d}tt|jfd|DS(sencode integer into base64 format :arg value: non-negative integer to encode :arg bits: number of bits to encode :returns: a string of length ``int(ceil(bits/6.0))``. iiic3`s|]}|?d@VqdS(i?N((R\RJ(R^(s8/usr/lib/python2.7/site-packages/passlib/utils/binary.pys "s(RhR RRRd(RoR^RRtitr((R^s8/usr/lib/python2.7/site-packages/passlib/utils/binary.pyt _encode_ints     cC`sP|dks|dkr'tdntr?|j||d!S|j|SdS(s0encodes 6-bit integer -> single hash64 characterii?svalue out of rangeiN(RDR RbRd(RoR^((s8/usr/lib/python2.7/site-packages/passlib/utils/binary.pyt encode_int6(s cC`sm|dks|dkr'tdn|d@|d?d@g}|jrWt|}ntt|j|S(s'encodes 12-bit integer -> 2 char stringiisvalue out of rangei?i(RDRhRRRRd(RoR^traw((s8/usr/lib/python2.7/site-packages/passlib/utils/binary.pyt encode_int121s  cC`s|dks|dkr'tdn|d@|d?d@|d?d@|d?d@g}|jrmt|}ntt|j|S(s'encodes 24-bit integer -> 4 char stringiisvalue out of rangei?ii i(RDRhRRRRd(RoR^R((s8/usr/lib/python2.7/site-packages/passlib/utils/binary.pyt encode_int24:s cC`s7|dks|dkr'tdn|j|dS(s&decode 5 char string -> 30 bit integerii?svalue out of rangei(RDR(RoR^((s8/usr/lib/python2.7/site-packages/passlib/utils/binary.pyt encode_int30DscC`s7|dks|dkr'tdn|j|dS(sencode 64-bit integer -> 11 char hash64 string this format is used primarily by des-crypt & variants to encode the DES output value used as a checksum. ilsvalue out of rangei@(RDR(RoR^((s8/usr/lib/python2.7/site-packages/passlib/utils/binary.pyt encode_int64JsN((t__name__t __module__t__doc__R2RbRhRdRgRjRlRRqtpropertyR[R|RmRiRRnRkRRRRRRRRRRRRRRRRRRRR(((s8/usr/lib/python2.7/site-packages/passlib/utils/binary.pyR&sD? "  ' +  # *  )    (      cB`s/eZdZdZdZdZdZRS(s<Base64Engine which delays initialization until it's accessedcO`s||f|_dS(N(t _lazy_opts(RoRtkwds((s8/usr/lib/python2.7/site-packages/passlib/utils/binary.pyRq\scC`s;|j\}}tt|j|||`t|_dS(N(RtsuperR'RqR&t __class__(RoRR((s8/usr/lib/python2.7/site-packages/passlib/utils/binary.pyt _lazy_init_scC`s,|jds|jntj||S(Nt_(t startswithRtobjectt__getattribute__(Rotattr((s8/usr/lib/python2.7/site-packages/passlib/utils/binary.pyRes N(RRRR2RRqRR(((s8/usr/lib/python2.7/site-packages/passlib/utils/binary.pyR'Xs   Rh(GRt __future__RRRtbase64RRRRXRROtbinasciiRRR RHtloggingt getLoggerRtlogtpasslibR tpasslib.utils.compatR R R RRRRRRRRRtpasslib.utils.decorRt__all__RRRRRRRRR R9tB_NULLRPR4R3R2R!R$R%RARGRFR"R#RVRWRR&R'R(RYR)R*(((s8/usr/lib/python2.7/site-packages/passlib/utils/binary.pytsn" R         !      ]