
    g_W                     8   S r SSKrSSKr\R                  " \5      rSSKJr  SSKJ	r	J
r
Jr  SSKJrJr  SSKJrJrJrJrJr  SSKJr  SSKJs  Jr  / SQrS	rS
 rS rS rS r  " S S\RB                  \RD                  \RF                  \RH                  5      r% " S S\RD                  \RL                  \RF                  \RH                  5      r' " S S\RF                  \RH                  5      r( " S S\RB                  \RF                  \RH                  5      r)g)zFpasslib.handlers.des_crypt - traditional unix (DES) crypt and variants    N)warn)
safe_crypt
test_crypt
to_unicode)h64h64big)byte_elem_valueuuascii_to_strunicodesuppress_cause)des_encrypt_int_block)	des_crypt
bsdi_cryptbigcryptcrypt16    c                 >    [        S [        U SS 5       5       5      $ )zconvert secret to 64-bit DES key.

this only uses the first 8 bytes of the secret,
and discards the high 8th bit of each byte at that.
a null parity bit is inserted after every 7th bit of the output.
c              3   T   #    U  H  u  p[        U5      S -  SUS-  -
  -  v   M      g7f)   9      N)r	   ).0ics      S/home/matz/Project1/venv/lib/python3.13/site-packages/passlib/handlers/des_crypt.py	<genexpr>'_crypt_secret_to_key.<locals>.<genexpr>(   s0      2041  "T)r!A#v60s   &(Nr   )sum	enumerate)secrets    r   _crypt_secret_to_keyr"      s)      2$VBQZ02 2 2    c                    [        U5      S:X  d   e[        R                  " U5      n[        U [        5      (       a  U R                  S5      n [        U [        5      (       d   e[        U ;   a#  [        R                  R                  [        5      e[        U 5      n[        USUS5      n[        R                  " U5      $ )z pure-python backed for des_crypt   utf-8r      )lenr   decode_int12
isinstancer   encodebytes_BNULLuhexcNullPasswordErrorr   r"   r   r   encode_int64)r!   salt
salt_value	key_valueresults        r   _raw_des_cryptr6   +   s    t9>> !!$'J &'""w'fe$$$$ ff&&y11 %V,I #9aR@F v&&r#   c                     [        U 5      nSn[        U 5      nX#:  a)  US-   n[        XU 5      n[        X5      U-  nUnX#:  a  M)  U$ )z,convert secret to DES key used by bsdi_cryptr   )r"   r(   r   )r!   r4   idxendnext	tmp_values         r   _bsdi_secret_to_keyr<   I   sZ    $V,I
C
f+C
)Qw(D)9:	))?)K		 )
 r#   c                 ^   [         R                  " U5      n[        U [        5      (       a  U R	                  S5      n [        U [
        5      (       d   e[        U ;   a#  [        R                  R                  [        5      e[        U 5      n[        USX15      n[        R                  " U5      $ )z"pure-python backend for bsdi_cryptr&   r   )r   decode_int24r*   r   r+   r,   r-   r.   r/   r0   r   r<   r   r   r1   )r!   roundsr2   r3   r4   r5   s         r   _raw_bsdi_cryptr@   U   s     !!$'J &'""w'fe$$$$ ff&&z22 $F+I #9aDF v&&r#   c                      \ rS rSrSrS rSr\R                  r	Sr
S=rr\R                  rSr\R                   " \" S5      \R$                  \R&                  -  5      r\S 5       rS	 rS
 rSr\S 5       rS r\S 5       rS rSrg)r   p   aG  This class implements the des-crypt password hash, and follows the :ref:`password-hash-api`.

It supports a fixed-length salt.

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

:type salt: str
:param salt:
    Optional salt string.
    If not specified, one will be autogenerated (this is recommended).
    If specified, it must be 2 characters, drawn from the regexp range ``[./0-9A-Za-z]``.

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

    .. versionadded:: 1.7

: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
r2   truncate_error   r%   r   zU
        ^
        (?P<salt>[./a-z0-9]{2})
        (?P<chk>[./a-z0-9]{11})?
        $c                 L    [        USS5      nUS S USS  p2U " X#=(       d    S S9$ )Nasciihashr%   r2   checksum)r   )clsrH   r2   chks       r   from_stringdes_crypt.from_string   s2    $0!Hd12hc{d33r#   c                 `    [        S5      U R                  U R                  4-  n[        U5      $ Nz%s%sr
   r2   rJ   r   selfrH   s     r   	to_stringdes_crypt.to_string   (    yDIIt}}55T""r#   c                 h    U R                   (       a  U R                  U5        U R                  U5      $ )N)use_defaults_check_truncate_policy_calc_checksum_backendrS   r!   s     r   _calc_checksumdes_crypt._calc_checksum   s*    ''/**622r#   os_cryptbuiltinc                 ^    [        SS5      (       a  U R                  U R                  5        gg)NtestabgOeLfPimXQoTFr   _set_calc_checksum_backend_calc_checksum_os_cryptrK   s    r   _load_backend_os_crypt des_crypt._load_backend_os_crypt   s)    fo..**3+F+FGr#   c                    [        XR                  5      nUc  U R                  U5      $ UR                  U R                  5      (       a  [	        U5      S:w  a*  [
        R                  R                  X R                  U5      eUSS  $ )N   r%   )r   r2   _calc_checksum_builtin
startswithr(   r.   r/   CryptBackendError)rS   r!   rH   s      r   rf   !des_crypt._calc_checksum_os_crypt   sm     &)),< ..v66tyy))SY"_&&**4DAAABxr#   c                 :    U R                  U R                  5        gNTre   rl   rg   s    r   _load_backend_builtindes_crypt._load_backend_builtin       &&s'A'ABr#   c                 h    [        XR                  R                  S5      5      R                  S5      $ NrG   )r6   r2   r+   decoder[   s     r   rl    des_crypt._calc_checksum_builtin   s'    fii&6&6w&?@GGPPr#    N)__name__
__module____qualname____firstlineno____doc__namesetting_kwdsr.   HASH64_CHARSchecksum_charschecksum_sizemin_salt_sizemax_salt_size
salt_charstruncate_sizerecompiler
   XI_hash_regexclassmethodrM   rT   r\   backendsrh   rf   rs   rl   __static_attributes__rz   r#   r   r   r   p   s    H D-L
 __NM
 %&%MMJ
 M **Q    ttBDDy	K 4 4
#3 'H
  
  Qr#   r   c                   V  ^  \ rS rSrSrS rSrSr\R                  r
S=rr\R                  rSrSrSrS	r\R&                  " \" S
5      \R*                  \R,                  -  5      r\S 5       rS rSr\U 4S j5       r\U 4S j5       rU 4S jrSr\S 5       r S r!\S 5       r"S r#Sr$U =r%$ )r      a  This class implements the BSDi-Crypt password hash, and follows the :ref:`password-hash-api`.

It supports a fixed-length salt, and a variable number of rounds.

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

:type salt: str
:param salt:
    Optional salt string.
    If not specified, one will be autogenerated (this is recommended).
    If specified, it must be 4 characters, drawn from the regexp range ``[./0-9A-Za-z]``.

:type rounds: int
:param rounds:
    Optional number of rounds to use.
    Defaults to 5001, must be between 1 and 16777215, inclusive.

: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 ``rounds``
    that are too small or too large, and ``salt`` strings that are too long.

    .. versionadded:: 1.6

.. versionchanged:: 1.6
    :meth:`hash` will now issue a warning if an even number of rounds is used
    (see :ref:`bsdi-crypt-security-issues` regarding weak DES keys).
)r2   r?   rE      i     i linearz
        ^
        _
        (?P<rounds>[./a-z0-9]{4})
        (?P<salt>[./a-z0-9]{4})
        (?P<chk>[./a-z0-9]{11})?
        $c                     [        USS5      nU R                  R                  U5      nU(       d  [        R                  R                  U 5      eUR                  SSS5      u  p4nU " [        R                  " UR                  S5      5      UUS9$ )NrG   rH   r?   r2   rL   )r?   r2   rJ   )
r   r   matchr.   r/   InvalidHashErrorgroupr   r>   r+   )rK   rH   mr?   r2   rL   s         r   rM   bsdi_crypt.from_string4  s}    $0OO!!$'&&))#..GGHfe<c##FMM'$:;
 	
r#   c                     [        S5      [        R                  " U R                  5      R	                  S5      U R
                  U R                  4-  n[        U5      $ )Nz_%s%s%srG   )r
   r   encode_int24r?   rx   r2   rJ   r   rR   s     r   rT   bsdi_crypt.to_stringA  sI    |s//<CCGL#yy$--9 9T""r#   Tc                    > [         [        U ]
  " S0 UD6nUR                  S-  (       d$  [	        S[
        R                  R                  5        U$ )Nr   zHbsdi_crypt rounds should be odd, as even rounds may reveal weak DES keysrz   )superr   usingdefault_roundsr   r.   r/   PasslibSecurityWarning)rK   kwdssubcls	__class__s      r   r   bsdi_crypt.usingN  sB    z3-55$$q([..0r#   c                 2   > [         [        U ]  5       nUS-  $ )Nr   )r   r   _generate_rounds)rK   r?   r   s     r   r   bsdi_crypt._generate_roundsW  s    z38: axr#   c                 V   > U R                   S-  (       d  g[        [        U ]  " S0 UD6$ )Nr   Trz   )r?   r   r   _calc_needs_update)rS   r   r   s     r   r   bsdi_crypt._calc_needs_updatee  s&    {{QZ9ADAAr#   r^   c                 ^    [        SS5      (       a  U R                  U R                  5        gg)Nrb   z_/...lLDAxARksGCHin.TFrd   rg   s    r   rh   !bsdi_crypt._load_backend_os_cryptt  s*    f455**3+F+FGr#   c                     U R                  5       n[        X5      nUc  U R                  U5      $ UR                  US S 5      (       a  [	        U5      S:w  a   [
        R                  R                  XU5      eUSS  $ )N	      )rT   r   rl   rm   r(   r.   r/   rn   )rS   r!   configrH   s       r   rf   "bsdi_crypt._calc_checksum_os_crypt|  sr    !&)< ..v66vbqz**c$i2o&&**4>>CDzr#   c                 :    U R                  U R                  5        grq   rr   rg   s    r   rs    bsdi_crypt._load_backend_builtin  ru   r#   c                 ~    [        XR                  U R                  R                  S5      5      R	                  S5      $ rw   )r@   r?   r2   r+   rx   r[   s     r   rl   !bsdi_crypt._calc_checksum_builtin  s.    v{{DII4D4DW4MNUUV]^^r#   rz   )&r{   r|   r}   r~   r   r   r   r   r.   r   r   r   r   r   r   
min_rounds
max_roundsrounds_costr   r   r
   r   r   r   r   rM   rT   _avoid_even_roundsr   r   r   r   rh   rf   rs   rl   r   __classcell__r   s   @r   r   r      s   F D%LM__N %&%MMJ NJJK **Q    ttBDDyK 

 

#    B 'H
  	  _ _r#   r   c                      ^  \ rS rSrSrS rSr\R                  r	S=r
r\R                  r\R                  " \" S5      \R                   \R"                  -  5      r\S 5       rS rSU 4S jjrS	 rS
rU =r$ )r   i  a'  This class implements the BigCrypt password hash, and follows the :ref:`password-hash-api`.

It supports a fixed-length salt.

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

:type salt: str
:param salt:
    Optional salt string.
    If not specified, one will be autogenerated (this is recommended).
    If specified, it must be 22 characters, drawn from the regexp range ``[./0-9A-Za-z]``.

: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
)r2   r%   zX
        ^
        (?P<salt>[./a-z0-9]{2})
        (?P<chk>([./a-z0-9]{11})+)?
        $c                     [        USS5      nU R                  R                  U5      nU(       d  [        R                  R                  U 5      eUR                  SS5      u  p4U " X4S9$ NrG   rH   r2   rL   rI   r   r   r   r.   r/   r   r   rK   rH   r   r2   rL   s        r   rM   bigcrypt.from_string  Y    $0OO!!$'&&))#..GGFE*	++r#   c                 `    [        S5      U R                  U R                  4-  n[        U5      $ rP   rQ   rR   s     r   rT   bigcrypt.to_string  rV   r#   c                    > [         [        U ]  XS9n[        U5      S-  (       a  [        R
                  R                  U 5      eU$ )N)relaxedrE   )r   r   _norm_checksumr(   r.   r/   r   )rS   rJ   r   r   s      r   r   bigcrypt._norm_checksum  s=    477Rx=2&&))$//r#   c                     [        U[        5      (       a  UR                  S5      n[        XR                  R                  S5      5      nSn[        U5      nX4:  a"  US-   nU[        XU USS 5      -  nUnX4:  a  M"  UR                  S5      $ )Nr&   rG   r   r   i)r*   r   r+   r6   r2   r(   rx   )rS   r!   rL   r8   r9   r:   s         r   r\   bigcrypt._calc_checksum  s    fg&&]]7+FVYY%5%5g%>?&ki7D>&T"2CBK@@CC i zz'""r#   rz   )F)r{   r|   r}   r~   r   r   r   r.   r   r   r   r   r   r   r   r
   r   r   r   r   rM   rT   r   r\   r   r   r   s   @r   r   r     s    4 DL__N %&%MMJ
 **Q    ttBDDy	K , ,#
# 
#r#   r   c                       \ rS rSrSrS rSrSr\R                  r
S=rr\R                  rSr\R                   " \" S5      \R$                  \R&                  -  5      r\S 5       rS	 rS
 rSrg)r   i  aD  This class implements the crypt16 password hash, and follows the :ref:`password-hash-api`.

It supports a fixed-length salt.

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

:type salt: str
:param salt:
    Optional salt string.
    If not specified, one will be autogenerated (this is recommended).
    If specified, it must be 2 characters, drawn from the regexp range ``[./0-9A-Za-z]``.

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

    .. versionadded:: 1.7

: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
rC      r%      zU
        ^
        (?P<salt>[./a-z0-9]{2})
        (?P<chk>[./a-z0-9]{22})?
        $c                     [        USS5      nU R                  R                  U5      nU(       d  [        R                  R                  U 5      eUR                  SS5      u  p4U " X4S9$ r   r   r   s        r   rM   crypt16.from_string+  r   r#   c                 `    [        S5      U R                  U R                  4-  n[        U5      $ rP   rQ   rR   s     r   rT   crypt16.to_string4  rV   r#   c                    [        U[        5      (       a  UR                  S5      nU R                  (       a  U R	                  U5         [
        R                  " U R                  R                  S5      5      n[        U5      n[        USUS5      n[        USS 5      n[        USUS5      n[        R                  " U5      [        R                  " U5      -   nUR                  S5      $ ! [         a    [        [        S5      5      ef = f)	Nr&   rG   zinvalid chars in saltr   r   r   r      )r*   r   r+   rX   rY   r   r)   r2   
ValueErrorr   r"   r   r   r1   rx   )rS   r!   r3   key1result1key2result2rL   s           r   r\   crypt16._calc_checksum;  s    fg&&]]7+F ''/	F))$))*:*:7*CDJ
 $F+ (aR@ $F1RL1 (aQ? !!'*V-@-@-IIzz'""#  	F ,C!DEE	Fs   
/C, ,Drz   N)r{   r|   r}   r~   r   r   r   r   r.   r   r   r   r   r   r   r   r   r
   r   r   r   r   rM   rT   r\   r   rz   r#   r   r   r     s    H D-L
 M__N
 %&%MMJ
 M
 **Q    ttBDDy	K , ,##r#   r   )*r   r   logging	getLoggerr{   logwarningsr   passlib.utilsr   r   r   passlib.utils.binaryr   r   passlib.utils.compatr	   r
   r   r   r   passlib.crypto.desr   passlib.utils.handlersutilshandlersr.   __all__r-   r"   r6   r<   r@   TruncateMixinHasManyBackendsHasSaltGenericHandlerr   	HasRoundsr   r   r   rz   r#   r   <module>r      s    L
 
 g''1  = < , [ [ 4 # # 
2'<
'6}Q  ""4"4bjj"BSBS }QF]_##R\\2::r?P?P ]_FM#rzz2,, M#fn#b

B,=,= n#r#   