o
    5h2                     @   sX  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	
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  mZ g d	ZG d
d dejZG dd dej ej!ej"Z#G dd deZ$G dd deZ%G dd de#Z&G dd de#Z'G dd de#Z(G dd de#Z)G dd deZ*dd eD Z+d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                   @   s6   e Zd ZdZdZdZdZejZ	e
dd Zdd ZdS )_Base64DigestHelperzhelper for ldap_md5 / ldap_sha1Nc                 C   s   | j S )z/tell StaticHandler to strip ident from checksum)identcls r!   q/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/passlib/handlers/ldap_digests.py_hash_prefix2   s   z _Base64DigestHelper._hash_prefixc                 C   s0   t |tr
|d}| | }t|dS )Nutf-8ascii)
isinstancer   encode
_hash_funcdigestr   decode)selfsecretchkr!   r!   r"   _calc_checksum7   s   

z"_Base64DigestHelper._calc_checksum)__name__
__module____qualname____doc__r   r(   _hash_regexuhPADDED_BASE64_CHARSchecksum_charsr   r#   r.   r!   r!   r!   r"   r   )   s    
r   c                   @   sV   e Zd ZdZdZejZdZdZ	dZ
d ZZdZdZdZedd Zdd	 Zd
d ZdS )_SaltedBase64DigestHelperz-helper for ldap_salted_md5 / ldap_salted_sha1)salt	salt_sizeN      c                 C   s   t |dd}| j|}|stj| zt|dd}W n t	y-   tj
| w | j}|s5J | |d | ||d  dS )Nr%   hashtmp)checksumr8   )r
   r3   matchr4   excInvalidHashErrorr   groupr'   	TypeErrorMalformedHashErrorchecksum_size)r    r<   mdatacsr!   r!   r"   from_stringN   s   z%_SaltedBase64DigestHelper.from_stringc                 C   s(   | j | j }| jt|d }t|S )Nr%   )r>   r8   r   r   r*   r   )r+   rG   r<   r!   r!   r"   	to_string\   s   z#_SaltedBase64DigestHelper.to_stringc                 C   s(   t |tr
|d}| || j  S )Nr$   )r&   r   r'   r(   r8   r)   )r+   r,   r!   r!   r"   r.   a   s   

z(_SaltedBase64DigestHelper._calc_checksum)r/   r0   r1   r2   setting_kwdsr4   r5   r6   r   r(   r3   min_salt_sizemax_salt_sizedefault_salt_sizeclassmethodrI   rJ   r.   r!   r!   r!   r"   r7   =   s    
r7   c                   @   .   e Zd ZdZd ZedZeZe	
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)r/   r0   r1   r2   namer   r   r   r(   recompiler3   r!   r!   r!   r"   r   i       r   c                   @   rP   )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)r/   r0   r1   r2   rQ   r   r   r   r(   rR   rS   r3   r!   r!   r!   r"   r   s   rT   r   c                   @   2   e Zd ZdZd ZedZdZeZ	e
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}r;   z+^\{SMD5\}(?P<tmp>[+/a-zA-Z0-9]{27,}={0,2})$N)r/   r0   r1   r2   rQ   r   r   rE   r   r(   rR   rS   r3   r!   r!   r!   r"   r   }   s     r   c                   @   rU   )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)r/   r0   r1   r2   rQ   r   r   rE   r   r(   rR   rS   r3   r!   r!   r!   r"   r      s    "r   c                   @   6   e Zd ZdZd ZedZdZdZe	Z
e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)r/   r0   r1   r2   rQ   r   r   rE   rN   r   r(   rR   rS   r3   r!   r!   r!   r"   r          r   c                   @   rW   )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}@   rY   z.^\{SSHA512\}(?P<tmp>[+/a-zA-Z0-9]{91,}={0,2})$N)r/   r0   r1   r2   rQ   r   r   rE   rN   r   r(   rR   rS   r3   r!   r!   r!   r"   r      rZ   r   c                   @   sH   e Zd ZdZd ZeedZe	j
dddedd Zed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                 C   s   dS )N!r!   r   r!   r!   r"   	genconfig:  s   zldap_plaintext.genconfigc                 C   s"   t |}t|o| j|d u S )N)r4   to_unicode_for_identifybool	_2307_patr?   )r    r<   r!   r!   r"   identifyA  s   
zldap_plaintext.identifyN)r/   r0   r1   r2   rQ   rR   rS   r   rb   r4   deprecated_methodrO   r_   rc   r!   r!   r!   r"   r      s    r   c                 C   s   g | ]}d | qS )ldap_r!   ).0rQ   r!   r!   r"   
<listcomp>L  s    rg   c                  C   s8   t  } tD ]}d| }tj||tddd| |< q~ d S )Nre   z{CRYPT}T)prefixlazy)globalsr	   r4   PrefixWrapperr   )gwnamerQ   r!   r!   r"   _init_ldap_crypt_handlersN  s
   rn   )-r2   base64r   r   hashlibr   r   r   r   logging	getLoggerr/   logrR   passlib.handlers.miscr   passlib.utilsr	   r
   passlib.utils.compatr   r   r   passlib.utils.decorr   passlib.utils.handlersutilshandlersr4   __all__StaticHandlerr   
HasRawSaltHasRawChecksumGenericHandlerr7   r   r   r   r   r   r   r   ldap_crypt_schemesrn   r!   r!   r!   r"   <module>   s.    ,

',((,
