
    g(U                     t   S r SSKrSSKr\R                  " \5      rSSKJrJrJ	r	J
r
  SSKJr  SSKJrJrJrJr  SSKJs  Jr  SS/rSrS	rS
rSrSS jr\" S5      r\" S5      r\" S5      r " S S\R>                  \R@                  \RB                  \RD                  5      r# " S S\#5      r$ " S S\#5      r%g)z9passlib.handlers.sha2_crypt - SHA256-Crypt / SHA512-Crypt    N)
safe_crypt
test_cryptrepeat_string
to_unicode)h64)byte_elem_valueuuascii_to_strunicodesha512_cryptsha256_crypt    ))r            r   r   )r      r   r   r   r   )   r   r   r   )r   r   r   r   r   )r   r   r   r   )r   r   r   r   r   )    
   r      r      r               r      r      r                                          	         )@*   r   r   r   +   r   r   r   ,   -   r    r   r   .   r!   r#   r   /   0   r(   r%   r'   1   r*   r,   r)   2   3   r/   r.   r   4   r0       r   5   6   !   r   r   7   "   #   r   8   9   $   r"   r$   :   %   &   r&   ;   <   '   r+   r-   =   (   )   r   >   ?   c                 F   [        U [        5      (       a  U R                  S5      n [        U [        5      (       d   e[        U ;   a4  [
        R                  R                  U(       a
  [        5      e[        5      e[        U 5      nSUs=::  a  S::  d   S5       e   S5       e[        U[        5      (       d   S5       eUR                  S5      n[        U5      nUS:  d   S5       eU(       a  [        R                  n[        nO[        R                  n[        nU" X-   U -   5      R!                  5       nU" X-   5      n	U	R"                  n
U
" [%        X5      5        UnU(       a"  U
" US	-  (       a  UOU 5        US	-  nU(       a  M"  U	R!                  5       nUS
:  a#  [%        U" X-  5      R!                  5       U5      nOPU" U 5      nUR"                  nUS	-
  nU(       a  U" U 5        US	-  nU(       a  M  [%        UR!                  5       U5      n[        U5      U:X  d   eU" US['        US   5      -   -  5      R!                  5       SU n[        U5      U:X  d   S5       eX-   nUU-   nUUUUU-   UU-   UU-   /n[(         VVs/ s H  u  nnUU   UU   4PM     nnnUn[+        US5      u  nnU(       aJ  U H6  u  nnU" UU" UU-   5      R!                  5       -   5      R!                  5       nM8     US	-  nU(       a  MJ  U(       am  US	-	  nUSU  H6  u  nnU" UU" UU-   5      R!                  5       -   5      R!                  5       nM8     US	-  (       a  U" UUU   S   -   5      R!                  5       n[,        R.                  " UU5      R1                  S5      $ s  snnf )a  perform raw sha256-crypt / sha512-crypt

this function provides a pure-python implementation of the internals
for the SHA256-Crypt and SHA512-Crypt algorithms; it doesn't
handle any of the parsing/validation of the hash strings themselves.

:arg pwd: password chars/bytes to hash
:arg salt: salt chars to use
:arg rounds: linear rounds cost
:arg use_512: use sha512-crypt instead of sha256-crypt mode

:returns:
    encoded checksum chars
zutf-8  ɚ;zinvalid roundszsalt not unicodeasciir&   zsalt too larger   `   r$   r   Nzsalt_len somehow > hash_len!r1   )
isinstancer   encodebytes_BNULLuhexcNullPasswordErrorr   r   lenhashlibsha512_512_transpose_mapsha256_256_transpose_mapdigestupdater   r   _c_digest_offsetsdivmodr   encode_transposed_bytesdecode)pwdsaltroundsuse_512pwd_lensalt_len
hash_consttranspose_mapdba_ctxa_ctx_updateidadptmp_ctxtmp_ctx_updatedsdp_dpdp_dspermsevenodddatadcblockstailpairss                              T/home/matz/Project1/venv/lib/python3.13/site-packages/passlib/handlers/sha2_crypt.py_raw_sha2_cryptr   8   s   B #wjj!c5!!!!}ff&&w|QQLQQ#hG 6&Y&8(88&8(88& dG$$8&88$;;wD4yHb=***= ^^
*^^
*
 
CJ$	%	,	,	.B sz"E<<L r+, 	A
1q5Rc*	a !
 
B | :cm4;;=wG S/ AI3FA a 7>>+W5r7g
 
DBA!778	9	@	@	B9H	MBr7h> >>\ EErEEuRxB59E 9JJ8I94eDk5:&8IDJ 
B&"%LFD
ID#C*R$Y"7">">"@@AHHJB ! & afuID#C*R$Y"7">">"@@AHHJB &
 !8BeQ/0779B
 &&r=9@@II3 Ks   Nzrounds=$0c                      ^  \ rS rSrSrSr\R                  rSr	\R                  r
SrSrSrSrS	rSrSU 4S
 jjrS rS r\S 5       rS rSrS	r\S 5       rS r\S 5       rS rSrU =r$ )_SHA2_Common   zBclass containing common code shared by sha256_crypt & sha512_crypt)rj   rk   implicit_rounds	salt_sizer$   rR   rS   linearFNc                    > [         [        U ]
  " S0 UD6  Uc"  U R                  =(       a    U R                  S:H  nXl        g )N   )superr   __init__use_defaultsrk   r   )selfr   kwds	__class__s      r   r   _SHA2_Common.__init__  s:    lD*2T2"#00HT[[D5HO.    c                 8    U R                  XR                  S L S9$ N)relaxed)
_norm_saltchecksum)r   rj   s     r   _parse_salt_SHA2_Common._parse_salt  s    t]]d-BCCr   c                 8    U R                  XR                  S L S9$ r   )_norm_roundsr   )r   rk   s     r   _parse_rounds_SHA2_Common._parse_rounds#  s      $1F GGr   c                    [        USS5      nU R                  nUR                  U5      (       d  [        R                  R                  U 5      e[        U5      S:X  d   eUSS  R                  [        5      nUS   R                  [        5      (       az  [        [        5      S:X  d   eUR                  S5      SS  nUR                  [        5      (       a)  U[        :w  a  [        R                  R                  U 5      e[        U5      nSnOSnSn[        U5      S	:X  a  Uu  pgO6[        U5      S
:X  a  US   nS nO[        R                  R                  U 5      eU " UUU=(       d    S US9$ )NrT   hashr   r   r'   Fr   Tr   r   )rk   rj   r   r   )r   ident
startswithrZ   r[   InvalidHashErrorr]   split_UDOLLAR_UROUNDSpop_UZEROZeroPaddedRoundsErrorintMalformedHashError)clsr   r   partsrk   r   rj   chks           r   from_string_SHA2_Common.from_string'  sH    $0		u%%&&))#..5zQQRx( 8x((x=A%%%YYq\!"%F  ((Vv-=ff22377[F#OF"O u:?ID#Z1_8DC&&++C00 [D+	 	r   c                 v   U R                   S:X  aS  U R                  (       aB  [        S5      U R                  U R                  U R
                  =(       d    [        S5      4-  nOL[        S5      U R                  U R                   U R                  U R
                  =(       d    [        S5      4-  n[        U5      $ )Nr   z%s%s$%s z%srounds=%d$%s$%s)rk   r   r	   r   rj   r   r
   )r   r   s     r   	to_string_SHA2_Common.to_stringT  s    ;;$4#7#7Y<4::tyy#'==#9AbE#; ;D ()TZZ-1YY8N2-P PDT""r   )os_cryptbuiltinc                 j    [        U R                  6 (       a  U R                  U R                  5        gg)NTF)r   
_test_hash_set_calc_checksum_backend_calc_checksum_os_cryptr   s    r   _load_backend_os_crypt#_SHA2_Common._load_backend_os_crypti  s(    s~~&**3+F+FGr   c                 "   U R                  5       n[        X5      nUc  U R                  U5      $ U R                  nUR	                  U R
                  5      (       a  X4* S-
     [        :w  a   [        R                  R                  XU5      eX4* S  $ )Nr   )
r   r   _calc_checksum_builtinchecksum_sizer   r   r   rZ   r[   CryptBackendError)r   secretconfigr   css        r   r   $_SHA2_Common._calc_checksum_os_cryptq  s    !&)< ..v66 tzz**d3q5kX.E&&**4>>CDzr   c                 :    U R                  U R                  5        g)NT)r   r   r   s    r   _load_backend_builtin"_SHA2_Common._load_backend_builtin  s    &&s'A'ABr   c                 X    [        XR                  U R                  U R                  5      $ N)r   rj   rk   _cdb_use_512)r   r   s     r   r   #_SHA2_Common._calc_checksum_builtin  s$    vyy$++#002 	2r   )r   r   )__name__
__module____qualname____firstlineno____doc__setting_kwdsrZ   HASH64_CHARSchecksum_charsmax_salt_size
salt_chars
min_rounds
max_roundsrounds_costr   _rounds_prefixr   r   r   r   classmethodr   r   backendsr   r   r   r   r   __static_attributes____classcell__)r   s   @r   r   r      s    L
 FL__N MJJJKLN
 O/DH * *X# 'H J "  2 2r   r   c                   8    \ rS rSrSrS r\" S5      rSrSr	Sr
Srg)	r   i  a  This class implements the SHA256-Crypt password hash, and follows the :ref:`password-hash-api`.

It supports a variable-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 0-16 characters, drawn from the regexp range ``[./0-9A-Za-z]``.

:type rounds: int
:param rounds:
    Optional number of rounds to use.
    Defaults to 535000, must be between 1000 and 999999999, inclusive.

    .. note::
        per the official specification, when the rounds parameter is set to 5000,
        it may be omitted from the hash string.

: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

..
    commented out, currently only supported by :meth:`hash`, and not via :meth:`using`:

    :type implicit_rounds: bool
    :param implicit_rounds:
        this is an internal option which generally doesn't need to be touched.

        this flag determines whether the hash should omit the rounds parameter
        when encoding it to a string; this is only permitted by the spec for rounds=5000,
        and the flag is ignored otherwise. the spec requires the two different
        encodings be preserved as they are, instead of normalizing them.
z$5$r2   i) )testz?$5$rounds=1000$test$QmQADEXMG8POI5WDsaeho0P36yK3Tcrgboabng6bkb/r   N)r   r   r   r   r   namer	   r   r   default_roundsr   r   r   r   r   r   r     s)    *\ DeHEMN
:Jr   c                   <    \ rS rSrSrS r\" S5      rSrSr	Sr
SrSrg	)
r   i  a  This class implements the SHA512-Crypt password hash, and follows the :ref:`password-hash-api`.

It supports a variable-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 0-16 characters, drawn from the regexp range ``[./0-9A-Za-z]``.

:type rounds: int
:param rounds:
    Optional number of rounds to use.
    Defaults to 656000, must be between 1000 and 999999999, inclusive.

    .. note::
        per the official specification, when the rounds parameter is set to 5000,
        it may be omitted from the hash string.

: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

..
    commented out, currently only supported by :meth:`hash`, and not via :meth:`using`:

    :type implicit_rounds: bool
    :param implicit_rounds:
        this is an internal option which generally doesn't need to be touched.

        this flag determines whether the hash should omit the rounds parameter
        when encoding it to a string; this is only permitted by the spec for rounds=5000,
        and the flag is ignored otherwise. the spec requires the two different
        encodings be preserved as they are, instead of normalizing them.
z$6$V   Ti
 )r   zj$6$rounds=1000$test$2M/Lx6MtobqjLjobw0Wmo4Q5OFx5nVLJvmgseatA6oMnyWeBdRDx4DU.1H3eGmse6pgsOgDisWBGI5c7TZauS0r   N)r   r   r   r   r   r   r	   r   r   r   r   r   r   r   r   r   r   r     s.    *^ DeHEMLN
(Jr   )F)&r   r^   logging	getLoggerr   logpasslib.utilsr   r   r   r   passlib.utils.binaryr   passlib.utils.compatr   r	   r
   r   passlib.utils.handlersutilshandlersrZ   __all__rY   re   rb   r`   r   r   r   r   HasManyBackends	HasRoundsHasSaltGenericHandlerr   r   r   r   r   r   <module>r      s    ?
  g''14 4 $8 8 # #  
   |JB Y<S6	
3L22%%r||RZZ$$L2d9:< 9:D=(< =(r   