
    XR_`0                        d Z ddl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 ddlmZ  ed          j        Zddlmc mZ g d	Z G d
 dej        ej        ej                  Z G d dej                  Z ej        deddd          Z G d dej        ej                  Z G 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                   l    e Zd ZdZd ZdZej        ZdZ	dZ
dZed             Zd ZdZe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                 *    |                                 S Nlowerclshashs     :/usr/lib/python3/dist-packages/passlib/handlers/windows.py
_norm_hashzlmhash._norm_hashU       zz||    c                     | j         r|                     |           t          |                     || j                                                d          S Nascii)use_defaults_check_truncate_policyr   rawencodingdecodeselfsecrets     r   _calc_checksumzlmhash._calc_checksumY   sM     	0''///txx6677>>wGGGr   s   KGS!@#$%Nc                    |s| j         }ddlm} | j        }t	          |t
                    r(|                                                    |          }n9t	          |t                    r|                                }nt          d          t          |d          } ||dd         |           ||dd         |          z   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,   MAGICs        r   r$   z
lmhash.rawc   s      	,+H 	988888
fg&& 	? \\^^**844FF&& 	?
 \\^^FF=>>>!&"--  !e44  "u556 	6r   r   )__name__
__module____qualname____doc__namesetting_kwdsuh	HEX_CHARSchecksum_charschecksum_sizetruncate_sizer.   classmethodr   r*   r0   r$    r   r   r
   r
      s         D D&L
 \NM
 M
 
   [H H H F#6 #6 #6 [#6 #6 #6r   r
   c                   r    e Zd ZdZd Zej        ZdZe	d             Z
d Ze	d             Ze	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                 *    |                                 S r   r   r   s     r   r   znthash._norm_hash   r   r   c                 l    t          |                     |                                        d          S r    )r   r$   r&   r'   s     r   r*   znthash._calc_checksum   s*    txx''((//888r   c                     t          |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	   r3   digest)r   r)   s     r   r$   z
nthash.raw   s>     FG8<<<6==--..55777r   Fc                     t          dt                     t                              |          }|r"t	          |                              d          n|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rets       r   
raw_nthashznthash.raw_nthash   sS     ?	! 	! 	! jj  /2;ws||""7+++;r   N)F)r7   r8   r9   r:   r;   r=   r>   r?   r@   rB   r   r*   r$   rQ   rC   r   r   r   r      s          D\NM
   [9 9 9 	8 	8 [	8 < < < [< < <r   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                   Z    e Zd ZdZd Zej        ZdZe	d             Z
d Ze	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                 *    |                                 S r   r   r   s     r   r   zmsdcc._norm_hash  r   r   c                 x    t          |                     || j                                                d          S r    r   r$   userr&   r'   s     r   r*   zmsdcc._calc_checksum
  .    txx	2233::7CCCr   c                 @   t          |dd                              d          }t          |dd                                                              d          }t          t          |                                          |z                                             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
        rH   r)   rI   rK   rY   )r   r3   r   r	   rL   )r   r)   rY   s      r   r$   z	msdcc.raw  s     FG8<<<CCKPP$v666<<>>EEkRR3v;;%%''$.//66888r   Nr7   r8   r9   r:   r;   r=   r>   r?   r@   rB   r   r*   r$   rC   r   r   r   r      sw         ( D\NM  [D D D 
9 
9 [
9 
9 
9r   r   c                   Z    e Zd ZdZd Zej        ZdZe	d             Z
d Ze	d             ZdS )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                 *    |                                 S r   r   r   s     r   r   zmsdcc2._norm_hash3  r   r   c                 x    t          |                     || j                                                d          S r    rX   r'   s     r   r*   zmsdcc2._calc_checksum7  rZ   r   c                 j   ddl m} t          |dd                              d          }t          |dd                                                              d          }t          t          |                                          |z                                             } |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_hmacrH   r)   rI   rK   rY   sha1i (     )passlib.crypto.digestra   r   r3   r   r	   rL   )r   r)   rY   ra   tmps        r   r$   z
msdcc2.raw:  s     	655555FG8<<<CCKPP$v666<<>>EEkRR#f++$$&&-..5577{63eR888r   Nr\   rC   r   r   r   r     sw         " D\NM  [D D D 9 9 [9 9 9r   r   )!r:   binasciir   logging	getLoggerr7   logwarningsr   passlib.utilsr   r   passlib.utils.compatr   rd   r   constr	   passlib.utils.handlersutilshandlersr=   __all__TruncateMixinHasEncodingContextStaticHandlerr
   r   PrefixWrapperr   HasUserContextr   r   rC   r   r   <module>rw      s   A A
       'g'11       7 6 6 6 6 6 6 6 ( ( ( ( ( ( - - - - - -k% # # # # # # # # #  k6 k6 k6 k6 k6Rr4b6F k6 k6 k6h-< -< -< -< -<R -< -< -<f RlF6		 	 	
T+9 +9 +9 +9 +9Br/ +9 +9 +9`-9 -9 -9 -9 -9R 0 -9 -9 -9 -9 -9r   