o
    5h`0                     @   s   d Z ddlmZ ddlZeeZddlmZ ddl	m
Z
mZ ddlmZ ddlmZ edjZddlm  mZ g d	ZG d
d dejejejZG dd dejZejdeddddZG dd dejejZG dd dejejZ dS )z;passlib.handlers.nthash - Microsoft Windows -related hashes    )hexlifyN)warn)
to_unicoderight_pad_string)unicode)lookup_hashmd4)lmhashnthash
bsd_nthashmsdccmsdcc2c                   @   sP   e Zd ZdZd ZdZejZdZ	dZ
dZedd Zdd	 Zd
ZedddZdS )r	   a  This class implements the Lan Manager Password hash, and follows the :ref:`password-hash-api`.

    It has no salt and a single fixed round.

    The :meth:`~passlib.ifc.PasswordHash.using` method accepts a single
    optional keyword:

    :param bool truncate_error:
        By default, this will silently truncate passwords larger than 14 bytes.
        Setting ``truncate_error=True`` will cause :meth:`~passlib.ifc.PasswordHash.hash`
        to raise a :exc:`~passlib.exc.PasswordTruncateError` instead.

        .. versionadded:: 1.7

    The :meth:`~passlib.ifc.PasswordHash.hash` and :meth:`~passlib.ifc.PasswordHash.verify` methods accept a single
    optional keyword:

    :type encoding: str
    :param encoding:

        This specifies what character encoding LMHASH should use when
        calculating digest. It defaults to ``cp437``, the most
        common encoding encountered.

    Note that while this class outputs digests in lower-case hexadecimal,
    it will accept upper-case as well.
    )truncate_error       cp437c                 C      |  S Nlowerclshash r   l/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/passlib/handlers/windows.py
_norm_hashU      zlmhash._norm_hashc                 C   s(   | j r| | t| || jdS Nascii)use_defaults_check_truncate_policyr   rawencodingdecodeselfsecretr   r   r   _calc_checksumY   s   
zlmhash._calc_checksums   KGS!@#$%Nc                 C   s   |s| j }ddlm} | j}t|tr| |}nt|tr%| }nt	dt
|d}||dd |||dd | S )a  encode password using LANMAN hash algorithm.

        :type secret: unicode or utf-8 encoded bytes
        :arg secret: secret to hash
        :type encoding: str
        :arg encoding:
            optional encoding to use for unicode inputs.
            this defaults to ``cp437``, which is the
            common case for most situations.

        :returns: returns string of raw bytes
        r   )des_encrypt_blockzsecret must be unicode or bytesr      )default_encodingpasslib.crypto.desr(   _magic
isinstancer   upperencodebytes	TypeErrorr   )r   r&   r"   r(   MAGICr   r   r   r!   c   s   



z
lmhash.rawr   )__name__
__module____qualname____doc__namesetting_kwdsuh	HEX_CHARSchecksum_charschecksum_sizetruncate_sizer*   classmethodr   r'   r,   r!   r   r   r   r   r	      s    "
r	   c                   @   sL   e Zd ZdZd ZejZdZe	dd Z
dd Ze	dd Ze	dd
dZdS )r
   a  This class implements the NT Password hash, and follows the :ref:`password-hash-api`.

    It has no salt and a single fixed round.

    The :meth:`~passlib.ifc.PasswordHash.hash` and :meth:`~passlib.ifc.PasswordHash.genconfig` methods accept no optional keywords.

    Note that while this class outputs lower-case hexadecimal digests,
    it will accept upper-case digests as well.
    r   c                 C   r   r   r   r   r   r   r   r      r   znthash._norm_hashc                 C   s   t | |dS r   )r   r!   r#   r$   r   r   r   r'      s   znthash._calc_checksumc                 C   s    t |ddd}t|d S )zencode password using MD4-based NTHASH algorithm

        :arg secret: secret as unicode or utf-8 encoded bytes

        :returns: returns string of raw bytes
        utf-8r&   param	utf-16-le)r   r   r/   digest)r   r&   r   r   r   r!      s   z
nthash.rawFc                 C   s*   t dt t|}|rt|dS |S )Nzfnthash.raw_nthash() is deprecated, and will be removed in Passlib 1.8, please use nthash.raw() insteadr   )r   DeprecationWarningr
   r!   r   r#   )r   r&   hexretr   r   r   
raw_nthash   s
   
znthash.raw_nthashN)F)r3   r4   r5   r6   r7   r9   r:   r;   r<   r>   r   r'   r!   rG   r   r   r   r   r
      s    

r
   r   z$3$$aZ  The class support FreeBSD's representation of NTHASH
    (which is compatible with the :ref:`modular-crypt-format`),
    and follows the :ref:`password-hash-api`.

    It has no salt and a single fixed round.

    The :meth:`~passlib.ifc.PasswordHash.hash` and :meth:`~passlib.ifc.PasswordHash.genconfig` methods accept no optional keywords.
    )prefixidentdocc                   @   >   e Zd ZdZd ZejZdZe	dd Z
dd Ze	dd Zd	S )
r   aK  This class implements Microsoft's Domain Cached Credentials password hash,
    and follows the :ref:`password-hash-api`.

    It has a fixed number of rounds, and uses the associated
    username as the salt.

    The :meth:`~passlib.ifc.PasswordHash.hash`, :meth:`~passlib.ifc.PasswordHash.genhash`, and :meth:`~passlib.ifc.PasswordHash.verify` methods
    have the following optional keywords:

    :type user: str
    :param user:
        String containing name of user account this password is associated with.
        This is required to properly calculate the hash.

        This keyword is case-insensitive, and should contain just the username
        (e.g. ``Administrator``, not ``SOMEDOMAIN\Administrator``).

    Note that while this class outputs lower-case hexadecimal digests,
    it will accept upper-case digests as well.
    r   c                 C   r   r   r   r   r   r   r   r     r   zmsdcc._norm_hashc                 C      t | || jdS r   r   r!   userr#   r$   r   r   r   r'   
     zmsdcc._calc_checksumc                 C   sD   t |dddd}t |ddd d}tt| |  S )zencode password using mscash v1 algorithm

        :arg secret: secret as unicode or utf-8 encoded bytes
        :arg user: username to use as salt

        :returns: returns string of raw bytes
        r?   r&   r@   rB   rN   )r   r/   r   r   rC   )r   r&   rN   r   r   r   r!     s   	z	msdcc.rawNr3   r4   r5   r6   r7   r9   r:   r;   r<   r>   r   r'   r!   r   r   r   r   r      s    
r   c                   @   rK   )
r   a  This class implements version 2 of Microsoft's Domain Cached Credentials
    password hash, and follows the :ref:`password-hash-api`.

    It has a fixed number of rounds, and uses the associated
    username as the salt.

    The :meth:`~passlib.ifc.PasswordHash.hash`, :meth:`~passlib.ifc.PasswordHash.genhash`, and :meth:`~passlib.ifc.PasswordHash.verify` methods
    have the following extra keyword:

    :type user: str
    :param user:
        String containing name of user account this password is associated with.
        This is required to properly calculate the hash.

        This keyword is case-insensitive, and should contain just the username
        (e.g. ``Administrator``, not ``SOMEDOMAIN\Administrator``).
    r   c                 C   r   r   r   r   r   r   r   r   3  r   zmsdcc2._norm_hashc                 C   rL   r   rM   r$   r   r   r   r'   7  rO   zmsdcc2._calc_checksumc                 C   s`   ddl m} t|dddd}t|ddd d}tt| |  }|d||d	d
S )zencode password using msdcc v2 algorithm

        :type secret: unicode or utf-8 bytes
        :arg secret: secret

        :type user: str
        :arg user: username to use as salt

        :returns: returns string of raw bytes
        r   )pbkdf2_hmacr?   r&   r@   rB   rN   sha1i (     )passlib.crypto.digestrQ   r   r/   r   r   rC   )r   r&   rN   rQ   tmpr   r   r   r!   :  s
   z
msdcc2.rawNrP   r   r   r   r   r     s    
r   )!r6   binasciir   logging	getLoggerr3   logwarningsr   passlib.utilsr   r   passlib.utils.compatr   rT   r   constr   passlib.utils.handlersutilshandlersr9   __all__TruncateMixinHasEncodingContextStaticHandlerr	   r
   PrefixWrapperr   HasUserContextr   r   r   r   r   r   <module>   s"    
t3*0