
    XR_2                        d Z ddlmZmZ ddlmZmZmZmZ ddl	Z	 e	j
        e          ZddlZddlmZ ddlmZmZ ddl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                  Z G d dej         ej!        ej"                  Z# G d de          Z$ G d de          Z% G d de#          Z& G d de#          Z' G d de#          Z( G d de#          Z) G d de          Z*d eD             Z+d Z, e,             dS )z.passlib.handlers.digests - plain hash digests
    )	b64encode	b64decode)md5sha1sha256sha512N)	plaintext)unix_crypt_schemes
to_unicode)uascii_to_strunicodeu)classproperty)ldap_plaintextldap_md5	ldap_sha1ldap_salted_md5ldap_salted_sha1ldap_salted_sha256ldap_salted_sha512ldap_des_cryptldap_bsdi_cryptldap_md5_cryptldap_sha1_cryptldap_bcryptldap_sha256_cryptldap_sha512_cryptc                   H    e Zd ZdZdZdZdZej        Z	e
d             Zd ZdS )_Base64DigestHelperzhelper for ldap_md5 / ldap_sha1Nc                     | j         S )z/tell StaticHandler to strip ident from checksum)identclss    ?/usr/lib/python3/dist-packages/passlib/handlers/ldap_digests.py_hash_prefixz _Base64DigestHelper._hash_prefix2   s     y    c                     t          |t                    r|                    d          }|                     |                                          }t          |                              d          S )Nutf-8ascii)
isinstancer   encode
_hash_funcdigestr   decode)selfsecretchks      r$   _calc_checksumz"_Base64DigestHelper._calc_checksum7   s\    fg&& 	,]]7++Foof%%,,..~~$$W---r&   )__name__
__module____qualname____doc__r!   r,   _hash_regexuhPADDED_BASE64_CHARSchecksum_charsr   r%   r2    r&   r$   r   r   )   s[        )) EJK+N  ]. . . . .r&   r   c                   f    e Zd ZdZdZej        ZdZdZ	dZ
dxZZdZdZdZed             Zd Zd ZdS )	_SaltedBase64DigestHelperz-helper for ldap_salted_md5 / ldap_salted_sha1)salt	salt_sizeN      c                    t          |dd          }| j                            |          }|st          j                            |           	 t          |                    d                              d                    }n-# t          $ r  t          j        
                    |           w xY w| j        }|sJ  | |d |         ||d                    S )Nr)   hashtmp)checksumr>   )r   r7   matchr8   excInvalidHashErrorr   groupr+   	TypeErrorMalformedHashErrorchecksum_size)r#   rC   mdatacss        r$   from_stringz%_SaltedBase64DigestHelper.from_stringN   s    $00O!!$'' 	/&))#...	1QWWU^^227;;<<DD 	1 	1 	1&++C000	1			sD"IDI6666s   5B *B.c                     | j         | j        z   }| j        t          |                              d          z   }t          |          S )Nr)   )rE   r>   r!   r   r.   r   )r/   rN   rC   s      r$   	to_stringz#_SaltedBase64DigestHelper.to_string\   s>    }ty(zIdOO227;;;T"""r&   c                     t          |t                    r|                    d          }|                     || j        z                                             S )Nr(   )r*   r   r+   r,   r>   r-   )r/   r0   s     r$   r2   z(_SaltedBase64DigestHelper._calc_checksuma   sJ    fg&& 	,]]7++Fv	12299;;;r&   )r3   r4   r5   r6   setting_kwdsr8   r9   r:   r!   r,   r7   min_salt_sizemax_salt_sizedefault_salt_sizeclassmethodrP   rR   r2   r;   r&   r$   r=   r=   =   s        77(L+NEJK$%%MM
 MM7 7 [7# # #
< < < < <r&   r=   c                   b    e Zd ZdZd Z ed          ZeZ e	j
         ed                    ZdS )r   zThis class stores passwords using LDAP's plain MD5 format, and follows the :ref:`password-hash-api`.

    The :meth:`~passlib.ifc.PasswordHash.hash` and :meth:`~passlib.ifc.PasswordHash.genconfig` methods have no optional keywords.
    z{MD5}z%^\{MD5\}(?P<chk>[+/a-zA-Z0-9]{22}==)$N)r3   r4   r5   r6   namer   r!   r   r,   recompiler7   r;   r&   r$   r   r   i   sM          DAgJJEJ"*QQGHHIIKKKr&   r   c                   b    e Zd ZdZd Z ed          ZeZ e	j
         ed                    ZdS )r   zThis class stores passwords using LDAP's plain SHA1 format, and follows the :ref:`password-hash-api`.

    The :meth:`~passlib.ifc.PasswordHash.hash` and :meth:`~passlib.ifc.PasswordHash.genconfig` methods have no optional keywords.
    z{SHA}z$^\{SHA\}(?P<chk>[+/a-zA-Z0-9]{27}=)$N)r3   r4   r5   r6   rZ   r   r!   r   r,   r[   r\   r7   r;   r&   r$   r   r   s   sM          DAgJJEJ"*QQFGGHHKKKr&   r   c                   f    e Zd ZdZd Z ed          ZdZeZ	 e
j         ed                    ZdS )r   a  This class stores passwords using LDAP's salted MD5 format, and follows the :ref:`password-hash-api`.

    It supports a 4-16 byte salt.

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

    :type salt: bytes
    :param salt:
        Optional salt string.
        If not specified, one will be autogenerated (this is recommended).
        If specified, it may be any 4-16 byte string.

    :type salt_size: int
    :param salt_size:
        Optional number of bytes to use when autogenerating new salts.
        Defaults to 4 bytes for compatibility with the LDAP spec,
        but some systems use larger salts, and Passlib supports
        any value between 4-16.

    :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

    .. versionchanged:: 1.6
        This format now supports variable length salts, instead of a fix 4 bytes.
    z{SMD5}rA   z+^\{SMD5\}(?P<tmp>[+/a-zA-Z0-9]{27,}={0,2})$N)r3   r4   r5   r6   rZ   r   r!   rL   r   r,   r[   r\   r7   r;   r&   r$   r   r   }   sS         @ DAhKKEMJ"*QQMNNOOKKKr&   r   c                   f    e Zd ZdZd Z ed          ZdZeZ	 e
j         ed                    ZdS )r   a  
    This class stores passwords using LDAP's "Salted SHA1" format,
    and follows the :ref:`password-hash-api`.

    It supports a 4-16 byte salt.

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

    :type salt: bytes
    :param salt:
        Optional salt string.
        If not specified, one will be autogenerated (this is recommended).
        If specified, it may be any 4-16 byte string.

    :type salt_size: int
    :param salt_size:
        Optional number of bytes to use when autogenerating new salts.
        Defaults to 4 bytes for compatibility with the LDAP spec,
        but some systems use larger salts, and Passlib supports
        any value between 4-16.

    :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

    .. versionchanged:: 1.6
        This format now supports variable length salts, instead of a fix 4 bytes.
    z{SSHA}   z+^\{SSHA\}(?P<tmp>[+/a-zA-Z0-9]{32,}={0,2})$N)r3   r4   r5   r6   rZ   r   r!   rL   r   r,   r[   r\   r7   r;   r&   r$   r   r      sS        ! !D DAhKKEMJ"*QQMNNOOKKKr&   r   c                   j    e Zd ZdZd Z ed          ZdZdZe	Z
 ej         ed                    ZdS )r   aC  
    This class stores passwords using LDAP's "Salted SHA2-256" format,
    and follows the :ref:`password-hash-api`.

    It supports a 4-16 byte salt.

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

    :type salt: bytes
    :param salt:
        Optional salt string.
        If not specified, one will be autogenerated (this is recommended).
        If specified, it may be any 4-16 byte string.

    :type salt_size: int
    :param salt_size:
        Optional number of bytes to use when autogenerating new salts.
        Defaults to 8 bytes for compatibility with the LDAP spec,
        but Passlib supports any value between 4-16.

    :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.7.3
    z	{SSHA256}       z.^\{SSHA256\}(?P<tmp>[+/a-zA-Z0-9]{48,}={0,2})$N)r3   r4   r5   r6   rZ   r   r!   rL   rW   r   r,   r[   r\   r7   r;   r&   r$   r   r      X         <  DAkNNEMJ"*QQPQQRRKKKr&   r   c                   j    e Zd ZdZd Z ed          ZdZdZe	Z
 ej         ed                    ZdS )r   aC  
    This class stores passwords using LDAP's "Salted SHA2-512" format,
    and follows the :ref:`password-hash-api`.

    It supports a 4-16 byte salt.

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

    :type salt: bytes
    :param salt:
        Optional salt string.
        If not specified, one will be autogenerated (this is recommended).
        If specified, it may be any 4-16 byte string.

    :type salt_size: int
    :param salt_size:
        Optional number of bytes to use when autogenerating new salts.
        Defaults to 8 bytes for compatibility with the LDAP spec,
        but Passlib supports any value between 4-16.

    :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.7.3
    z	{SSHA512}@   rc   z.^\{SSHA512\}(?P<tmp>[+/a-zA-Z0-9]{91,}={0,2})$N)r3   r4   r5   r6   rZ   r   r!   rL   rW   r   r,   r[   r\   r7   r;   r&   r$   r   r      rd   r&   r   c                       e Zd ZdZd Z ej         ed                    Z e	j
        dd          ed                         Zed             ZdS )	r   aY  This class stores passwords in plaintext, and follows the :ref:`password-hash-api`.

    This class acts much like the generic :class:`!passlib.hash.plaintext` handler,
    except that it will identify a hash only if it does NOT begin with the ``{XXX}`` identifier prefix
    used by RFC2307 passwords.

    The :meth:`~passlib.ifc.PasswordHash.hash`, :meth:`~passlib.ifc.PasswordHash.genhash`, and :meth:`~passlib.ifc.PasswordHash.verify` methods all require the
    following additional contextual keyword:

    :type encoding: str
    :param encoding:
        This controls the character encoding to use (defaults to ``utf-8``).

        This encoding will be used to encode :class:`!unicode` passwords
        under Python 2, and decode :class:`!bytes` hashes under Python 3.

    .. versionchanged:: 1.6
        The ``encoding`` keyword was added.
    z^\{\w+\}.*$z1.7z2.0)
deprecatedremovedc                     dS )N!r;   r"   s    r$   	genconfigzldap_plaintext.genconfig:  s	    
 sr&   c                     t          j        |          }t          |          o| j                            |          d u S )N)r8   to_unicode_for_identifybool	_2307_patrF   )r#   rC   s     r$   identifyzldap_plaintext.identifyA  s:     )$//Dzz?cm11$774??r&   N)r3   r4   r5   r6   rZ   r[   r\   r   rp   r8   deprecated_methodrX   rl   rq   r;   r&   r$   r   r      s         , D
11^,,--IRUE:::  [ ;:
 @ @ [@ @ @r&   r   c                     g | ]}d |z   S )ldap_r;   ).0rZ   s     r$   
<listcomp>rv   L  s    FFF$w~FFFr&   c                      t                      } t          D ]/}d|z   }t          j        ||t	          d          d          | |<   0~ d S )Nrt   z{CRYPT}T)prefixlazy)globalsr
   r8   PrefixWrapperr   )gwnamerZ   s      r$   _init_ldap_crypt_handlersr~   N  sS     			A# P P"4q||$OOO$	r&   )-r6   base64r   r   hashlibr   r   r   r   logging	getLoggerr3   logr[   passlib.handlers.miscr	   passlib.utilsr
   r   passlib.utils.compatr   r   r   passlib.utils.decorr   passlib.utils.handlersutilshandlersr8   __all__StaticHandlerr   
HasRawSaltHasRawChecksumGenericHandlerr=   r   r   r   r   r   r   r   ldap_crypt_schemesr~   r;   r&   r$   <module>r      s    ( ' ' ' ' ' ' ' - - - - - - - - - - - - 'g'11 				 , + + + + + 8 8 8 8 8 8 8 8 : : : : : : : : : : - - - - - - # # # # # # # # #  ,. . . . ."* . . .('< '< '< '< '<r/@"BS '< '< '<XJ J J J J" J J JI I I I I# I I I%P %P %P %P %P/ %P %P %PN(P (P (P (P (P0 (P (P (PX%S %S %S %S %S2 %S %S %SP%S %S %S %S %S2 %S %S %SP%@ %@ %@ %@ %@Y %@ %@ %@X GF2DFFF 
 
 
       r&   