
    XR_BJ                        d Z ddlmZmZ ddlmZmZ ddlZ ej        e	          Z
ddlmZ ddlmZmZ ddlmZmZmZmZ ddlmZ ddlmc mZ g d	Z G d
 dej        ej        ej        ej                  Z dde	fdZ! e!ddd ed                    Z" e!ddd          Z# e!ddd          Z$ ej%        de"ddd          Z& ej%        de#ddd          Z' ej%        de$d d!d          Z(d"Z) G d# d$ej        ej        ej        ej                  Z* G d% d&ej        ej+        ej                  Z, G d' d(ej        ej        ej                  Z- G d) d*ej        ej        ej        ej                  Z.dS )+z,passlib.handlers.pbkdf - PBKDF2 based hashes    )hexlify	unhexlify)	b64encode	b64decodeN)
to_unicode)ab64_decodeab64_encode)str_to_basciiuuascii_to_strunicode)pbkdf2_hmac)pbkdf2_sha1pbkdf2_sha256pbkdf2_sha512cta_pbkdf2_sha1dlitz_pbkdf2_sha1grub_pbkdf2_sha512c                   b    e Zd ZdZdZej        ZdZdZ	dZ
dZdZdZdZed	             Zd
 Zd ZdS )Pbkdf2DigestHandlerz1base class for various pbkdf2_{digest} algorithmssalt	salt_sizerounds      N       linearc                     t          j        || j        |           \  }}}t          |                    d                    }|r"t          |                    d                    } | |||          S )N)handlerasciir   r   checksum)uh	parse_mc3identr   encodeclshashr   r   chks        9/usr/lib/python3/dist-packages/passlib/handlers/pbkdf2.pyfrom_stringzPbkdf2DigestHandler.from_string?   sr    Lsy#FFFc4;;w//00 	3cjj1122Cs&tc::::    c                     t          | j                                      d          }t          | j                                      d          }t	          j        | j        | j        ||          S Nr"   )r	   r   decoder$   r%   
render_mc3r'   r   selfr   r,   s      r-   	to_stringzPbkdf2DigestHandler.to_stringG   sV    49%%,,W55$-((//88}TZdC@@@r/   c                 P    t          | j        || j        | j        | j                  S )N)r   _digestr   r   checksum_sizer5   secrets     r-   _calc_checksumz"Pbkdf2DigestHandler._calc_checksumL   s!    4<DKI[\\\r/   )__name__
__module____qualname____doc__setting_kwdsr%   HASH64_CHARSchecksum_charsdefault_salt_sizemax_salt_sizedefault_rounds
min_rounds
max_roundsrounds_costr8   classmethodr.   r6   r<    r/   r-   r   r      s        ;; 3L_N M NJJK G ; ; [;A A A
] ] ] ] ]r/   r   i.  c                     d| z   }|t          d          | fz  }t          }t          ||ft          |||| |||dz  dz   dz  dt          |                                 |j        |          z  	                    S )
z;create new Pbkdf2DigestHandler subclass for a specific hashpbkdf2_Nz$pbkdf2-%s$         a$  This class implements a generic ``PBKDF2-HMAC-%(digest)s``-based password hash, and follows the :ref:`password-hash-api`.

    It supports a variable-length salt, and a variable number of rounds.

    The :meth:`~passlib.ifc.PasswordHash.using` method accepts the following optional keywords:

    :type salt: bytes
    :param salt:
        Optional salt bytes.
        If specified, the length must be between 0-1024 bytes.
        If not specified, a %(dsc)d byte salt will be autogenerated (this is recommended).

    :type salt_size: int
    :param salt_size:
        Optional number of bytes to use when autogenerating new salts.
        Defaults to %(dsc)d bytes, but can be any value between 0 and 1024.

    :type rounds: int
    :param rounds:
        Optional number of rounds to use.
        Defaults to %(dr)d, but must be within ``range(1,1<<32)``.

    :type relaxed: bool
    :param relaxed:
        By default, providing an invalid value for one of the other
        keywords will result in a :exc:`ValueError`. If ``relaxed=True``,
        and the error can be corrected, a :exc:`~passlib.exc.PasslibHashWarning`
        will be issued instead. Correctable errors include ``rounds``
        that are too small or too large, and ``salt`` strings that are too long.

        .. versionadded:: 1.6
    )digestdscdr)r>   namer'   r8   rF   r9   encoded_checksum_sizer@   )r   r   typedictupperrD   )	hash_namedigest_sizer   r'   modulerT   bases          r-   create_pbkdf2_hashr]   P   s    y D}-  I</Dtgt!*1}Q2> ioo''T-C
O
O
O?P(  (  (  ( ( (r/   sha1   i z$pbkdf2$)r'   sha256    iHq  sha512@   ia  ldap_pbkdf2_sha1z{PBKDF2}Tldap_pbkdf2_sha256z{PBKDF2-SHA256}z$pbkdf2-sha256$ldap_pbkdf2_sha512z{PBKDF2-SHA512}z$pbkdf2-sha512$s   -_c                   x    e Zd ZdZd ZdZ ed          ZdZdZ	dZ
ej        ZdZdZd	Zed
             Zd Zd ZdS )r   a  This class implements Cryptacular's PBKDF2-based crypt algorithm, and follows the :ref:`password-hash-api`.

    It supports a variable-length salt, and a variable number of rounds.

    The :meth:`~passlib.ifc.PasswordHash.using` method accepts the following optional keywords:

    :type salt: bytes
    :param salt:
        Optional salt bytes.
        If specified, it may be any length.
        If not specified, a one will be autogenerated (this is recommended).

    :type salt_size: int
    :param salt_size:
        Optional number of bytes to use when autogenerating new salts.
        Defaults to 16 bytes, but can be any value between 0 and 1024.

    :type rounds: int
    :param rounds:
        Optional number of rounds to use.
        Defaults to 60000, must be within ``range(1,1<<32)``.

    :type relaxed: bool
    :param relaxed:
        By default, providing an invalid value for one of the other
        keywords will result in a :exc:`ValueError`. If ``relaxed=True``,
        and the error can be corrected, a :exc:`~passlib.exc.PasslibHashWarning`
        will be issued instead. Correctable errors include ``rounds``
        that are too small or too large, and ``salt`` strings that are too long.

        .. versionadded:: 1.6
    r   $p5k2$r_   r   r   r   r   r   c                    t          j        || j        d|           \  }}}t          |                    d          t
                    }|r(t          |                    d          t
                    } | |||          S )Nr   )rounds_baser!   r"   r#   )r%   r&   r'   r   r(   CTA_ALTCHARSr)   s        r-   r.   zcta_pbkdf2_sha1.from_string   s{     LsybRUVVVcW--|<< 	?CJJw//>>Cs&tc::::r/   c                     t          | j        t                                        d          }t          | j        t                                        d          }t          j        | j        | j        ||d          S )Nr"   r   rj   )	r   r   rk   r2   r$   r%   r3   r'   r   r4   s      r-   r6   zcta_pbkdf2_sha1.to_string   s_    L1188AA|44;;GDD}TZdCRPPPPr/   c                 <    t          d|| j        | j        d          S )Nr^   r_   r   r   r   r:   s     r-   r<   zcta_pbkdf2_sha1._calc_checksum   s    6649dk2FFFr/   N)r=   r>   r?   r@   rT   rA   r   r'   r9   rD   rE   r   rF   rG   rH   rI   rJ   r.   r6   r<   rK   r/   r-   r   r      s         J D2LAhKKEM M !/NJJK ; ; [;Q Q QG G G G Gr/   r   c                       e Zd ZdZd ZdZ ed          Z ed          ZdZ	dZ
ej        Zej        ZdZdZd	Zed
             Zd Zd Zd ZdS )r   aC  This class implements Dwayne Litzenberger's PBKDF2-based crypt algorithm, and follows the :ref:`password-hash-api`.

    It supports a variable-length salt, and a variable number of rounds.

    The :meth:`~passlib.ifc.PasswordHash.using` method accepts the following optional keywords:

    :type salt: str
    :param salt:
        Optional salt string.
        If specified, it may be any length, but must use the characters in the regexp range ``[./0-9A-Za-z]``.
        If not specified, a 16 character salt will be autogenerated (this is recommended).

    :type salt_size: int
    :param salt_size:
        Optional number of bytes to use when autogenerating new salts.
        Defaults to 16 bytes, but can be any value between 0 and 1024.

    :type rounds: int
    :param rounds:
        Optional number of rounds to use.
        Defaults to 60000, must be within ``range(1,1<<32)``.

    :type relaxed: bool
    :param relaxed:
        By default, providing an invalid value for one of the other
        keywords will result in a :exc:`ValueError`. If ``relaxed=True``,
        and the error can be corrected, a :exc:`~passlib.exc.PasslibHashWarning`
        will be issued instead. Correctable errors include ``rounds``
        that are too small or too large, and ``salt`` strings that are too long.

        .. versionadded:: 1.6
    r   rh   z1000000000000000000000000000000000000000000000000=r   r   r   r   r   c                 b    t          j        || j        dd|           \  }}} | |||          S )Nr     )rj   rF   r!   r#   )r%   r&   r'   r)   s        r-   r.   zdlitz_pbkdf2_sha1.from_string9  sF    Lsyb8;SJ J Jcs&tc::::r/   c                 p    | j         }|dk    rd }t          j        | j        || j        | j        d          S Nrr   r   rm   )r   r%   r3   r'   r   r$   r5   r   s     r-   r6   zdlitz_pbkdf2_sha1.to_string?  s9    S==F}TZDMWYZZZZr/   c                 f    | j         }|dk    rd }t          j        | j        || j        d d          S rt   )r   r%   r3   r'   r   ru   s     r-   _get_configzdlitz_pbkdf2_sha1._get_configE  s6    S==F}TZDbQQQQr/   c                     |                                  }t          d||| j        d          }t          |                              d          S )Nr^      r"   )rw   r   r   r	   r2   )r5   r;   r   results       r-   r<   z dlitz_pbkdf2_sha1._calc_checksumN  sF    !!VVT4;CC6""))'222r/   N)r=   r>   r?   r@   rT   rA   r   r'   _stub_checksumrD   rE   r%   rB   
salt_charsr   rF   rG   rH   rI   rJ   r.   r6   rw   r<   rK   r/   r-   r   r      s         J D2LAhKKEQ~&&N MJ
 !/NJJK ; ; [;
[ [ [R R R3 3 3 3 3r/   r   c                   ^    e Zd ZdZd ZdZ ed          ZdZdxZ	Z
ed             Zd Zd Zd	S )
atlassian_pbkdf2_sha1aB  This class implements the PBKDF2 hash used by Atlassian.

    It supports a fixed-length salt, and a fixed number of rounds.

    The :meth:`~passlib.ifc.PasswordHash.using` method accepts the following optional keywords:

    :type salt: bytes
    :param salt:
        Optional salt bytes.
        If specified, the length must be exactly 16 bytes.
        If not specified, a salt will be autogenerated (this is recommended).

    :type relaxed: bool
    :param relaxed:
        By default, providing an invalid value for one of the other
        keywords will result in a :exc:`ValueError`. If ``relaxed=True``,
        and the error can be corrected, a :exc:`~passlib.exc.PasslibHashWarning`
        will be issued instead. Correctable errors include
        ``salt`` strings that are too long.

        .. versionadded:: 1.6
    )r   z	{PKCS5S2}ra   r   c                 J   t          |dd          }| j        }|                    |          st          j                            |           t          |t          |          d                              d                    }|d d         |dd          }} | ||          S )Nr"   r+   r   )r   r$   )	r   r'   
startswithr%   excInvalidHashErrorr   lenr(   )r*   r+   r'   datar   r,   s         r-   r.   z!atlassian_pbkdf2_sha1.from_string{  s    $00	u%% 	/&))#...c%jjkk*11'::;;"ItBCCycss++++r/   c                     | j         | j        z   }| j        t          |                              d          z   }t          |          S r1   )r   r$   r'   r   r2   r   )r5   r   r+   s      r-   r6   zatlassian_pbkdf2_sha1.to_string  s>    y4=(zIdOO227;;;T"""r/   c                 2    t          d|| j        dd          S )Nr^   i'  ra   )r   r   r:   s     r-   r<   z$atlassian_pbkdf2_sha1._calc_checksum  s     6649eR@@@r/   N)r=   r>   r?   r@   rT   rA   r   r'   r9   min_salt_sizerE   rJ   r.   r6   r<   rK   r/   r-   r~   r~   [  s         . #DLAkNNEM %'&MM, , [,# # #
A A A A Ar/   r~   c                   x    e Zd ZdZd ZdZ ed          ZdZdZ	dZ
ej        ZdZdZdZed	             Zd
 Zd ZdS )r   a  This class implements Grub's pbkdf2-hmac-sha512 hash, and follows the :ref:`password-hash-api`.

    It supports a variable-length salt, and a variable number of rounds.

    The :meth:`~passlib.ifc.PasswordHash.using` method accepts the following optional keywords:

    :type salt: bytes
    :param salt:
        Optional salt bytes.
        If specified, the length must be between 0-1024 bytes.
        If not specified, a 64 byte salt will be autogenerated (this is recommended).

    :type salt_size: int
    :param salt_size:
        Optional number of bytes to use when autogenerating new salts.
        Defaults to 64 bytes, but can be any value between 0 and 1024.

    :type rounds: int
    :param rounds:
        Optional number of rounds to use.
        Defaults to 19000, but must be within ``range(1,1<<32)``.

    :type relaxed: bool
    :param relaxed:
        By default, providing an invalid value for one of the other
        keywords will result in a :exc:`ValueError`. If ``relaxed=True``,
        and the error can be corrected, a :exc:`~passlib.exc.PasslibHashWarning`
        will be issued instead. Correctable errors include ``rounds``
        that are too small or too large, and ``salt`` strings that are too long.

        .. versionadded:: 1.6
    r   zgrub.pbkdf2.sha512.rc   r   r   r   r   c                    t          j        || j        t          d          |           \  }}}t	          |                    d                    }|r"t	          |                    d                    } | |||          S )N.)sepr!   r"   r#   )r%   r&   r'   r   r   r(   r)   s        r-   r.   zgrub_pbkdf2_sha512.from_string  s    Lsyaff146 6 6cW--.. 	1CJJw//00Cs&tc::::r/   c           	      F   t          | j                                      d                                          }t          | j                                      d                                          }t          j        | j        | j        ||t          d                    S )Nr"   r   )r   )
r   r   r2   rX   r$   r%   r3   r'   r   r   r4   s      r-   r6   zgrub_pbkdf2_sha512.to_string  sy    ty!!((117799dm$$++G44::<<}TZdCQsVVLLLLr/   c                 <    t          d|| j        | j        d          S )Nrb   rc   ro   r:   s     r-   r<   z!grub_pbkdf2_sha512._calc_checksum  s     8VTYRHHHr/   N)r=   r>   r?   r@   rT   rA   r   r'   r9   rD   rE   r   rF   rG   rH   rI   rJ   r.   r6   r<   rK   r/   r-   r   r     s         @  D2LA#$$EM M"1NJJK; ; [;M M M
I I I I Ir/   r   )/r@   binasciir   r   base64r   r   logging	getLoggerr=   logpasslib.utilsr   passlib.utils.binaryr   r	   passlib.utils.compatr
   r   r   r   passlib.crypto.digestr   passlib.utils.handlersutilshandlersr%   __all__	HasRounds
HasRawSaltHasRawChecksumGenericHandlerr   r]   r   r   r   PrefixWrapperrd   re   rf   rk   r   HasSaltr   r~   r   rK   r/   r-   <module>r      s   2 2
 ( ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' 'g'11 % $ $ $ $ $ 9 9 9 9 9 9 9 9 I I I I I I I I I I I I - - - - - - # # # # # # # # #  1] 1] 1] 1] 1]",r7H"J[ 1] 1] 1]f 7<4PX . . . .f ! V11Z==III""8R77""8R77#2#$6ZQ[cghhh %R%&:MK\^ow{||| %R%&:MK\^ow{|||  WG WG WG WG WGblBM23DbFW WG WG WG@`3 `3 `3 `3 `3bj"2C `3 `3 `3R3A 3A 3A 3A 3ABM2+<b>O 3A 3A 3ApDI DI DI DI DIr}b6GIZ DI DI DI DI DIr/   