Ë
    àU.jœ?  ã                   ó>  — d Z ddlmZm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mZmZ ddlmZ ddlmZmZmZmZmZmZ ddlmc mZ g d	¢Zd
Z G d„ dej@                  ejB                  «      Z" G d„ de"«      Z# G d„ dejH                  «      Z%y)z1
passlib.handlers.cisco -- Cisco password hashes
é    )ÚhexlifyÚ	unhexlify)Úmd5N)Úwarn)Úright_pad_stringÚ
to_unicodeÚrepeat_stringÚto_bytes)Úh64)ÚunicodeÚuÚjoin_byte_valuesÚjoin_byte_elemsÚiter_byte_valuesÚuascii_to_str)Ú	cisco_pixÚ	cisco_asaÚcisco_type7s    ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿc                   óF   — e Zd ZdZd ZdZdZdZdZe	j                  ZdZd„ Zy)r   aš  
    This class implements the password hash used by older Cisco PIX firewalls,
    and follows the :ref:`password-hash-api`.
    It does a single round of hashing, and relies on the username
    as the salt.

    This class only allows passwords <= 16 bytes, anything larger
    will result in a :exc:`~passlib.exc.PasswordSizeError` if passed to :meth:`~cisco_pix.hash`,
    and be silently rejected if passed to :meth:`~cisco_pix.verify`.

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

    :param str user:
        String containing name of user account this password is associated with.

        This is *required* in order to correctly hash passwords associated
        with a user account on the Cisco device, as it is used to salt
        the hash.

        Conversely, this *must* be omitted or set to ``""`` in order to correctly
        hash passwords which don't have an associated user account
        (such as the "enable" password).

    .. versionadded:: 1.6

    .. versionchanged:: 1.7.1

        Passwords > 16 bytes are now rejected / throw error instead of being silently truncated,
        to match Cisco behavior.  A number of :ref:`bugs <passlib-asa96-bug>` were fixed
        which caused prior releases to generate unverifiable hashes in certain cases.
    é   TFc                 óâ  — | j                   }t        |t        «      r|j                  d«      }d}t	        |«      | j
                  kD  r[| j                  rFd| j                  | j
                  fz  }t        j                  j                  | j
                  |¬«      ‚|t        z   }| j                  }|r@t        |t        «      r|j                  d«      }|rt	        |«      dk  r|t        |d«      z  }|rt	        |«      dkD  rd}nd}t        ||«      }|r||z  }t        |«      j!                  «       }t#        d	„ t%        |«      D «       «      }t'        j(                  |«      j+                  d
«      S )a7  
        This function implements the "encrypted" hash format used by Cisco
        PIX & ASA. It's behavior has been confirmed for ASA 9.6,
        but is presumed correct for PIX & other ASA releases,
        as it fits with known test vectors, and existing literature.

        While nearly the same, the PIX & ASA hashes have slight differences,
        so this function performs differently based on the _is_asa class flag.
        Noteable changes from PIX to ASA include password size limit
        increased from 16 -> 32, and other internal changes.
        úutf-8Nz.Password too long (%s allows at most %d bytes))Úmsgé   é   r   é    c              3   ó8   K  — | ]  \  }}|d z   dz  sŒ|–— Œ y­w)é   é   N© )Ú.0ÚiÚcs      úXC:\xampp\htdocs\tradingbinance\backend\.venv\Lib\site-packages\passlib/handlers/cisco.pyÚ	<genexpr>z+cisco_pix._calc_checksum.<locals>.<genexpr>Þ   s!   è ø€ Ð PÑ/@¡t q¨!ÀQÈÁUÈaÃK¤Ñ/@ùs   ‚“Úascii)Ú_is_asaÚ
isinstancer   ÚencodeÚlenÚtruncate_sizeÚuse_defaultsÚnameÚuhÚexcÚPasswordSizeErrorÚ_DUMMY_BYTESÚuserr	   r   r   Údigestr   Ú	enumerater   Úencode_bytesÚdecode)ÚselfÚsecretÚasaÚspoil_digestr   r2   Úpad_sizer3   s           r$   Ú_calc_checksumzcisco_pix._calc_checksumg   sR  € ð �l‰lˆô �fœgÔ&Ø—]‘] 7Ó+ˆFð, ˆÜˆv‹;˜×+Ñ+Ò+Ø× Ò àFØ—y‘y $×"4Ñ"4Ð5ñ6�ä—f‘f×.Ñ.¨t×/AÑ/AÀsÐ.ÓKÐKð  &¬Ñ4�ð. �y‰yˆÙÜ˜$¤Ô(Ø—{‘{ 7Ó+�Ùœ#˜f›+¨Ò*Øœ-¨¨aÓ0Ñ0�ñ ”3�v“; Ò#Ø‰HàˆHÜ! &¨(Ó3ˆñ
 à�lÑ"ˆFÜ�V“×#Ñ#Ó%ˆô !Ñ P¬y¸Ô/@Ó PÓPˆô
 ×Ñ Ó'×.Ñ.¨wÓ7Ð7ó    N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r-   r+   Útruncate_errorÚtruncate_verify_rejectÚchecksum_sizer.   ÚHASH64_CHARSÚchecksum_charsr'   r<   r    r=   r$   r   r   $   s>   „ ñ!ðR €Dà€Mð €NØ!Ðð
 €MØ—_‘_€Nð €Gó
|8r=   r   c                   ó   — e Zd ZdZd ZdZdZy)r   aä  
    This class implements the password hash used by Cisco ASA/PIX 7.0 and newer (2005).
    Aside from a different internal algorithm, it's use and format is identical
    to the older :class:`cisco_pix` class.

    For passwords less than 13 characters, this should be identical to :class:`!cisco_pix`,
    but will generate a different hash for most larger inputs
    (See the `Format & Algorithm`_ section for the details).

    This class only allows passwords <= 32 bytes, anything larger
    will result in a :exc:`~passlib.exc.PasswordSizeError` if passed to :meth:`~cisco_asa.hash`,
    and be silently rejected if passed to :meth:`~cisco_asa.verify`.

    .. versionadded:: 1.7

    .. versionchanged:: 1.7.1

        Passwords > 32 bytes are now rejected / throw error instead of being silently truncated,
        to match Cisco behavior.  A number of :ref:`bugs <passlib-asa96-bug>` were fixed
        which caused prior releases to generate unverifiable hashes in certain cases.
    r   TN)r>   r?   r@   rA   r-   r+   r'   r    r=   r$   r   r   ù   s   „ ñð8 €Dð
 €Mð
 �Gr=   r   c                   óÒ   ‡ — e Zd ZdZd ZdZej                  ZdZ	dZ
edˆ fd„	«       Zed„ «       Zdˆ fd„	Zedd„«       Zed	„ «       Zd
„ Zd„ Zedd„«       Z ed«      Zed„ «       Zˆ xZS )r   a+  
    This class implements the "Type 7" password encoding used by Cisco IOS,
    and follows the :ref:`password-hash-api`.
    It has a simple 4-5 bit salt, but is nonetheless a reversible encoding
    instead of a real hash.

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

    :type salt: int
    :param salt:
        This may be an optional salt integer drawn from ``range(0,16)``.
        If omitted, one will be chosen at random.

    :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`` values that are out of range.

    Note that while this class outputs digests in upper-case hexadecimal,
    it will accept lower-case as well.

    This class also provides the following additional method:

    .. automethod:: decode
    ©Úsaltr   é4   c                 óž   •‡— t        t        | �
  di |¤Ž}‰�5|j                  ‰|j	                  d«      ¬«      Št        ˆfd„«      |_        |S )NÚrelaxed)rM   c                  ó   •— ‰ S ©Nr    rI   s   €r$   Ú<lambda>z#cisco_type7.using.<locals>.<lambda>f  s   ø€ ¹r=   r    )Úsuperr   ÚusingÚ
_norm_saltÚgetÚstaticmethodÚ_generate_salt)ÚclsrJ   ÚkwdsÚsubclsÚ	__class__s    `  €r$   rR   zcisco_type7.usinga  sM   ù€ ä”{ CÑ.Ñ6°Ñ6ˆØÐØ×$Ñ$ T°4·8±8¸IÓ3FÐ$ÓGˆDÜ$0³Ó$>ˆFÔ!Øˆr=   c                 óÈ   — t        |dd«      }t        |«      dk  rt        j                  j	                  | «      ‚t        |d d «      } | ||dd  j                  «       ¬«      S )Nr&   Úhashé   )rJ   Úchecksum)r   r*   r.   r/   ÚInvalidHashErrorÚintÚupper)rW   r\   rJ   s      r$   Úfrom_stringzcisco_type7.from_stringi  sY   € ä˜$ ¨Ó0ˆÜˆt‹9�qŠ=Ü—&‘&×)Ñ)¨#Ó.Ð.Ü�4˜˜�8‹}ˆÙ˜ t¨A¨B x§~¡~Ó'7Ô8Ð8r=   c                 óú   •— t        t        | �
  di |¤Ž |�| j                  |«      }|| _        y | j                  r.| j                  «       }| j                  |«      |k(  sJ d|›�«       ‚t        d«      ‚|| _        y )Nzgenerated invalid salt: zno salt specifiedr    )rQ   r   Ú__init__rS   r,   rV   Ú	TypeErrorrJ   )r7   rJ   rX   rZ   s      €r$   rd   zcisco_type7.__init__q  s~   ø€ ÜŒk˜4Ñ)Ñ1¨DÒ1ØÐØ—?‘? 4Ó(ˆDð ˆ�	ð ×ÒØ×&Ñ&Ó(ˆDØ—?‘? 4Ó(¨DÒ0ÑXÑRVÐ2XÓXÐ0äÐ/Ó0Ð0Øˆ�	r=   c                 ó  — t        |t        «      s!t        j                  j	                  |dd«      ‚d|cxk  r| j
                  k  r|S  d}|r-t        |t        j                  «       |dk  rdS | j
                  S t        |«      ‚)z�
        validate & normalize salt value.
        .. note::
            the salt for this algorithm is an integer 0-52, not a string
        ÚintegerrJ   r   z"salt/offset must be in 0..52 range)	r(   r`   r.   r/   ÚExpectedTypeErrorÚmax_salt_valuer   ÚPasslibHashWarningÚ
ValueError)rW   rJ   rM   r   s       r$   rS   zcisco_type7._norm_salt|  s}   € ô ˜$¤Ô$Ü—&‘&×*Ñ*¨4°¸FÓCÐCØ�Ô*˜×*Ñ*Ò*ØˆKð +à2ˆÙÜ�”b×+Ñ+Ô,Ø˜qš�1Ð8 c×&8Ñ&8Ð8ä˜S“/Ð!r=   c                  óB   — t         j                  j                  dd«      S )Nr   é   )r.   ÚrngÚrandintr    r=   r$   rV   zcisco_type7._generate_saltŽ  s   € ä�v‰v�~‰~˜a Ó$Ð$r=   c                 óJ   — d| j                   t        | j                  «      fz  S )Nz%02d%s)rJ   r   r^   )r7   s    r$   Ú	to_stringzcisco_type7.to_string’  s   € Ø˜4Ÿ9™9¤m°D·M±MÓ&BÐCÑCÐCr=   c                 óÈ   — t        |t        «      r|j                  d«      }t        | j	                  || j
                  «      «      j                  d«      j                  «       S )Nr   r&   )r(   r   r)   r   Ú_cipherrJ   r6   ra   )r7   r8   s     r$   r<   zcisco_type7._calc_checksum•  sJ   € ô �fœgÔ&Ø—]‘] 7Ó+ˆFÜ�t—|‘| F¨D¯I©IÓ6Ó7×>Ñ>¸wÓG×MÑMÓOÐOr=   c                 óÎ   — | j                  |«      }t        |j                  j                  d«      «      }|j	                  ||j
                  «      }|r|j                  |«      S |S )zÈdecode hash, returning original password.

        :arg hash: encoded password
        :param encoding: optional encoding to use (defaults to ``UTF-8``).
        :returns: password as unicode
        r&   )rb   r   r^   r)   rs   rJ   r6   )rW   r\   Úencodingr7   ÚtmpÚraws         r$   r6   zcisco_type7.decodeœ  sU   € ð �‰˜tÓ$ˆÜ˜Ÿ™×,Ñ,¨WÓ5Ó6ˆØ�l‰l˜3 §	¡	Ó*ˆÙ'/ˆs�z‰z˜(Ó#Ð8°SÐ8r=   z5dsfd;kfoA,.iyewrkldJKDHSUBsgvca69834ncxv9873254k;fg87c                 ó†   ‡‡‡— | j                   Št        ‰«      Št        ˆˆˆfd„t        t	        |«      «      D «       «      S )z1xor static key against data - encrypts & decryptsc              3   óR   •K  — | ]  \  }}|t        ‰‰|z   ‰z     «      z  –— Œ  y ­wrO   )Úord)r!   ÚidxÚvalueÚkeyÚkey_sizerJ   s      €€€r$   r%   z&cisco_type7._cipher.<locals>.<genexpr>±  s5   øè ø€ ð  
á?‘
��Uð ”C˜˜T C™Z¨8Ñ3Ñ4Ó5Õ5Ù?ùs   ƒ$')Ú_keyr*   r   r4   r   )rW   ÚdatarJ   r}   r~   s     `@@r$   rs   zcisco_type7._cipher¬  s<   ú€ ð �h‰hˆÜ�s“8ˆÜõ  
ä'Ô(8¸Ó(>Ô?ó 
ó 
ð 	
r=   rO   )F)r   )r>   r?   r@   rA   r-   Úsetting_kwdsr.   ÚUPPER_HEX_CHARSrF   Úmin_salt_valueri   ÚclassmethodrR   rb   rd   rS   rU   rV   rq   r<   r6   r   r   rs   Ú__classcell__)rZ   s   @r$   r   r   )  sÂ   ø„ ñðF €DØ€Lð
 ×'Ñ'€Nð €NØ€Nð
 ôó ðð ñ9ó ð9õ	ð ò"ó ð"ð" ñ%ó ð%òDòPð ò
9ó ð
9ñ ÐDÓE€Dàñ
ó ô
r=   r   )&rA   Úbinasciir   r   Úhashlibr   ÚloggingÚ	getLoggerr>   ÚlogÚwarningsr   Úpasslib.utilsr   r   r	   r
   Úpasslib.utils.binaryr   Úpasslib.utils.compatr   r   r   r   r   r   Úpasslib.utils.handlersÚutilsÚhandlersr.   Ú__all__r1   ÚHasUserContextÚStaticHandlerr   r   ÚGenericHandlerr   r    r=   r$   Ú<module>r–      s’   ðñ÷ (Ý Û Ð'�g×'Ñ'¨Ó1�Ý ÷ PÓ OÝ $÷>÷ >ç #Ð #ò€ð €ô
8�×!Ñ! 2×#3Ñ#3ô 8ôj'�	ô 'ô`K
�"×#Ñ#õ K
r=   