
     h_W                     8   d Z ddlZddlZ ej                  e      ZddlmZ ddlm	Z	m
Z
mZ ddlmZmZ ddlmZmZmZmZmZ ddlmZ ddlmc mZ g dZd	Zd
 Zd Zd Zd Z  G d dejB                  ejD                  ejF                  ejH                        Z% G d dejD                  ejL                  ejF                  ejH                        Z' G d dejF                  ejH                        Z( G d dejB                  ejF                  ejH                        Z)y)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                 >    t        d t        | dd       D              S )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   P   K   | ]  \  }}t        |      d z  d|dz  z
  z     yw)   9      N)r	   ).0ics      \/var/www/html/Resume-Scraper/venv/lib/python3.12/site-packages/passlib/handlers/des_crypt.py	<genexpr>z'_crypt_secret_to_key.<locals>.<genexpr>(   s3      21a  "T)r!A#v6 2s   $&Nr   )sum	enumerate)secrets    r   _crypt_secret_to_keyr!      s)      2$VBQZ02 2 2    c                 d   t        |      dk(  sJ t        j                  |      }t        | t              r| j                  d      } t        | t              sJ t        | v r#t        j                  j                  t              t        |       }t        |d|d      }t        j                  |      S )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_cryptr5   +   s    t9> !!$'J &'"w'fe$$ ff&&y11 %V,I #9aR@F v&&r"   c                     t        |       }d}t        |       }||k  r*|dz   }t        | ||       }t        ||      |z  }|}||k  r*|S )z,convert secret to DES key used by bsdi_cryptr   )r!   r'   r   )r    r3   idxendnext	tmp_values         r   _bsdi_secret_to_keyr;   I   sb    $V,I
C
f+C
)Qw(D)9:	))Y?)K		 )
 r"   c                 D   t        j                  |      }t        | t              r| j	                  d      } t        | t
              sJ t        | v r#t        j                  j                  t              t        |       }t        |d||      }t        j                  |      S )z"pure-python backend for bsdi_cryptr%   r   )r   decode_int24r)   r   r*   r+   r,   r-   r.   r/   r   r;   r   r   r0   )r    roundsr1   r2   r3   r4   s         r   _raw_bsdi_cryptr?   U   s     !!$'J &'"w'fe$$ ff&&z22 $F+I #9aVDF v&&r"   c                      e Zd ZdZd ZdZej                  ZdZ	dxZ
Zej                  ZdZ ej                   ed      ej"                  ej$                  z        Zed        Zd Zd	 Zd
Zed        Zd Zed        Zd Zy)r   a  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
    r1   truncate_error   r$   r   zU
        ^
        (?P<salt>[./a-z0-9]{2})
        (?P<chk>[./a-z0-9]{11})?
        $c                 L    t        |dd      }|d d |dd  }} | ||xs d       S )Nasciihashr$   r1   checksum)r   )clsrF   r1   chks       r   from_stringzdes_crypt.from_string   s6    $0!Hd12hcs{d33r"   c                 `    t        d      | j                  | j                  fz  }t        |      S Nz%s%sr
   r1   rH   r   selfrF   s     r   	to_stringzdes_crypt.to_string   (    yDIIt}}55T""r"   c                 ^    | j                   r| j                  |       | j                  |      S )N)use_defaults_check_truncate_policy_calc_checksum_backendrP   r    s     r   _calc_checksumzdes_crypt._calc_checksum   s*    ''/**622r"   os_cryptbuiltinc                 T    t        dd      r| j                  | j                         yy)NtestabgOeLfPimXQoTFr   _set_calc_checksum_backend_calc_checksum_os_cryptrI   s    r   _load_backend_os_cryptz des_crypt._load_backend_os_crypt   s&    fo.**3+F+FGr"   c                    t        || j                        }|| j                  |      S |j                  | j                        rt	        |      dk7  r+t
        j                  j                  | | j                  |      |dd  S )N   r$   )r   r1   _calc_checksum_builtin
startswithr'   r-   r.   CryptBackendError)rP   r    rF   s      r   ra   z!des_crypt._calc_checksum_os_crypt   sn     &$)),< ..v66tyy)SY"_&&**4DAAABxr"   c                 :    | j                  | j                         yNTr`   rf   rb   s    r   _load_backend_builtinzdes_crypt._load_backend_builtin       &&s'A'ABr"   c                 j    t        || j                  j                  d            j                  d      S NrE   )r5   r1   r*   decoderW   s     r   rf   z des_crypt._calc_checksum_builtin   s)    fdii&6&6w&?@GGPPr"   N)__name__
__module____qualname____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classmethodrK   rQ   rX   backendsrc   ra   rl   rf    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                   H    e Zd ZdZd ZdZdZej                  Z	dxZ
Zej                  ZdZdZdZdZ ej$                   ed	      ej(                  ej*                  z        Zed
        Zd ZdZe fd       Ze fd       Z fdZdZed        Zd Z ed        Z!d Z" xZ#S )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).
    )r1   r>   rC      i     i linearz
        ^
        _
        (?P<rounds>[./a-z0-9]{4})
        (?P<salt>[./a-z0-9]{4})
        (?P<chk>[./a-z0-9]{11})?
        $c                    t        |dd      }| j                  j                  |      }|st        j                  j                  |       |j                  ddd      \  }}} | t        j                  |j                  d            ||      S )NrE   rF   r>   r1   rJ   )r>   r1   rH   )
r   r   matchr-   r.   InvalidHashErrorgroupr   r=   r*   )rI   rF   mr>   r1   rJ   s         r   rK   zbsdi_crypt.from_string4  s    $0OO!!$'&&))#..GGHfe<c##FMM'$:;
 	
r"   c                     t        d      t        j                  | j                        j	                  d      | j
                  | j                  fz  }t        |      S )Nz_%s%s%srE   )r
   r   encode_int24r>   rp   r1   rH   r   rO   s     r   rQ   zbsdi_crypt.to_stringA  sI    |s//<CCGL#yy$--9 9T""r"   Tc                     t        t        | 
  di |}|j                  dz  s$t	        dt
        j                  j                         |S )Nr   zHbsdi_crypt rounds should be odd, as even rounds may reveal weak DES keysr   )superr   usingdefault_roundsr   r-   r.   PasslibSecurityWarning)rI   kwdssubcls	__class__s      r   r   zbsdi_crypt.usingN  sB    z3-55$$q([..0r"   c                 2    t         t        |          }|dz  S )Nr   )r   r   _generate_rounds)rI   r>   r   s     r   r   zbsdi_crypt._generate_roundsW  s    z38: axr"   c                 J    | j                   dz  syt        t        |   di |S )Nr   Tr   )r>   r   r   _calc_needs_update)rP   r   r   s     r   r   zbsdi_crypt._calc_needs_updatee  s&    {{QZ9ADAAr"   rY   c                 T    t        dd      r| j                  | j                         yy)Nr]   z_/...lLDAxARksGCHin.TFr_   rb   s    r   rc   z!bsdi_crypt._load_backend_os_cryptt  s'    f45**3+F+FGr"   c                     | j                         }t        ||      }|| j                  |      S |j                  |d d       rt	        |      dk7  r!t
        j                  j                  | ||      |dd  S )N	      )rQ   r   rf   rg   r'   r-   r.   rh   )rP   r    configrF   s       r   ra   z"bsdi_crypt._calc_checksum_os_crypt|  ss    !&&)< ..v66vbqz*c$i2o&&**4>>CDzr"   c                 :    | j                  | j                         yrj   rk   rb   s    r   rl   z bsdi_crypt._load_backend_builtin  rm   r"   c                     t        || j                  | j                  j                  d            j	                  d      S ro   )r?   r>   r1   r*   rp   rW   s     r   rf   z!bsdi_crypt._calc_checksum_builtin  s0    vt{{DII4D4DW4MNUUV]^^r"   )$rq   rr   rs   rt   ru   rv   ry   r-   rw   rx   rz   r{   r|   r   
min_rounds
max_roundsrounds_costr~   r   r
   r   r   r   r   rK   rQ   _avoid_even_roundsr   r   r   r   rc   ra   rl   rf   __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                        e Zd ZdZd ZdZej                  ZdxZ	Z
ej                  Z ej                   ed      ej                  ej                   z        Zed        Zd Zd	 fd	Zd Z xZS )
r   ag  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
    )r1   r$   zX
        ^
        (?P<salt>[./a-z0-9]{2})
        (?P<chk>([./a-z0-9]{11})+)?
        $c                     t        |dd      }| j                  j                  |      }|st        j                  j                  |       |j                  dd      \  }} | ||      S NrE   rF   r1   rJ   rG   r   r   r   r-   r.   r   r   rI   rF   r   r1   rJ   s        r   rK   zbigcrypt.from_string  ]    $0OO!!$'&&))#..GGFE*	cs++r"   c                 `    t        d      | j                  | j                  fz  }t        |      S rM   rN   rO   s     r   rQ   zbigcrypt.to_string  rR   r"   c                     t         t        |   ||      }t        |      dz  rt        j
                  j                  |       |S )N)relaxedrC   )r   r   _norm_checksumr'   r-   r.   r   )rP   rH   r   r   s      r   r   zbigcrypt._norm_checksum  s?    47'7Rx=2&&))$//r"   c                    t        |t              r|j                  d      }t        || j                  j                  d            }d}t        |      }||k  r"|dz   }|t        ||| |dd       z  }|}||k  r"|j                  d      S )Nr%   rE   r   r   i)r)   r   r*   r5   r1   r'   rp   )rP   r    rJ   r7   r8   r9   s         r   rX   zbigcrypt._calc_checksum  s    fg&]]7+FVTYY%5%5g%>?&kCi7D>&T"2CBK@@CC Ci zz'""r"   )F)rq   rr   rs   rt   ru   rv   r-   rw   rx   rz   r{   r|   r~   r   r
   r   r   r   r   rK   rQ   r   rX   r   r   s   @r   r   r     s    4 DL__N %&%MMJ
 "**Q    ttBDDy	K , ,#
#r"   r   c                       e Zd ZdZd ZdZdZej                  Z	dxZ
Zej                  ZdZ ej                   ed      ej"                  ej$                  z        Zed        Zd Zd	 Zy
)r   a  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
    rA      r$      zU
        ^
        (?P<salt>[./a-z0-9]{2})
        (?P<chk>[./a-z0-9]{22})?
        $c                     t        |dd      }| j                  j                  |      }|st        j                  j                  |       |j                  dd      \  }} | ||      S r   r   r   s        r   rK   zcrypt16.from_string+  r   r"   c                 `    t        d      | j                  | j                  fz  }t        |      S rM   rN   rO   s     r   rQ   zcrypt16.to_string4  rR   r"   c                     t        |t              r|j                  d      }| j                  r| j	                  |       	 t        j                  | j                  j                  d            }t        |      }t        |d|d      }t        |dd       }t        |d|d      }t        j                  |      t        j                  |      z   }|j                  d      S # t        $ r t        t        d            w xY w)	Nr%   rE   zinvalid chars in saltr   r   r   r      )r)   r   r*   rT   rU   r   r(   r1   
ValueErrorr   r!   r   r   r0   rp   )rP   r    r2   key1result1key2result2rJ   s           r   rX   z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 C=N)rq   rr   rs   rt   ru   rv   ry   r-   rw   rx   rz   r{   r|   r}   r~   r   r
   r   r   r   r   rK   rQ   rX   r   r"   r   r   r     s    H D-L
 M__N
 %&%MMJ
 M
 "**Q    ttBDDy	K , ,##r"   r   )*rt   r~   logging	getLoggerrq   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!   r5   r;   r?   TruncateMixinHasManyBackendsHasSaltGenericHandlerr   	HasRoundsr   r   r   r   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"   