
    ;OOf(U                     r   d Z ddlZddlZ ej                  e      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 ddlmc mZ ddgZdZd	Zd
ZdZddZ ed      Z ed      Z ed      Z G d dej>                  ej@                  ejB                  ejD                        Z# G d de#      Z$ G d de#      Z%y)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                    t        | t              r| j                  d      } t        | t              sJ t        | v r/t
        j                  j                  |r
t              t              t        |       }d|cxk  rdk  sJ d        J d       t        |t              sJ d       |j                  d      }t        |      }|dk  sJ d       |rt        j                  }t        }nt        j                  }t        } || |z   | z         j!                         } || |z         }	|	j"                  }
 |
t%        ||             |}|r |
|d	z  r|n|        |d	z  }|r|	j!                         }|d
k  r$t%         || |z        j!                         |      }nE ||       }|j"                  }|d	z
  }|r ||        |d	z  }|rt%        |j!                         |      }t        |      |k(  sJ  ||dt'        |d         z   z        j!                         d| }t        |      |k(  sJ d       ||z   }||z   }|||||z   ||z   ||z   g}t(        D cg c]  \  }}||   ||   f }}}|}t+        |d      \  }}|rB|D ]5  \  }} || |||z         j!                         z         j!                         }7 |d	z  }|rB|rf|d	z	  }|d| D ]5  \  }} || |||z         j!                         z         j!                         }7 |d	z  r ||||   d   z         j!                         }t-        j.                  ||      j1                  d      S c c}}w )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                              \/var/www/premiumrankchecker/venv/lib/python3.12/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=***= ^^
*^^
*
 
C$J$	%	,	,	.B sTz"E<<L r7+, 	A
1q5Rc*	a 
 
B | :cGm4;;=wG S/ AI3FA  7>>+W5r7g
 
DBA!778	9	@	@	B9H	MBr7h> >>\ rEErEEuRxB59E 9JJ94eDk5:&JDJ 
B&"%LFD
 	KID#C*R$Y"7">">"@@AHHJB	K!  afu 	KID#C*R$Y"7">">"@@AHHJB	K
 !8BeQ/0779B
 &&r=9@@II3 Ks   M	zrounds=$0c                        e Zd ZdZdZej                  ZdZej                  Z	dZ
dZdZdZdZdZd fd		Zd
 Zd Zed        Zd ZdZdZed        Zd Zed        Zd Z xZS )_SHA2_CommonzBclass containing common code shared by sha256_crypt & sha512_crypt)rj   rk   implicit_rounds	salt_sizer$   rR   rS   linearFNc                 x    t        t        | 
  di | || j                  xr | j                  dk(  }|| _        y )N   )superr   __init__use_defaultsrk   r   )selfr   kwds	__class__s      r   r   z_SHA2_Common.__init__  s<    lD*2T2"#00HT[[D5HO.    c                 @    | j                  || j                  d u       S N)relaxed)
_norm_saltchecksum)r   rj   s     r   _parse_saltz_SHA2_Common._parse_salt  s    tT]]d-BCCr   c                 @    | j                  || j                  d u       S r   )_norm_roundsr   )r   rk   s     r   _parse_roundsz_SHA2_Common._parse_rounds#  s!      $1F GGr   c                    t        |dd      }| j                  }|j                  |      st        j                  j                  |       t        |      dk(  sJ |dd  j                  t              }|d   j                  t              rst        t              dk(  sJ |j                  d      dd  }|j                  t              r(|t        k7  rt        j                  j                  |       t        |      }d}nd}d}t        |      d	k(  r|\  }}n5t        |      d
k(  r|d   }d }nt        j                  j                  |        | |||xs d |      S )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_stringz_SHA2_Common.from_string'  s?    $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                 j   | j                   dk(  rS| j                  rGt        d      | j                  | j                  | j
                  xs t        d      fz  }t        |      S t        d      | j                  | j                   | j                  | j
                  xs t        d      fz  }t        |      S )Nr   z%s%s$%s z%srounds=%d$%s$%s)rk   r   r	   r   rj   r   r
   )r   r   s     r   	to_stringz_SHA2_Common.to_stringT  s    ;;$4#7#7Y<4::tyy#'==#9AbE#; ;D
 T"" ()TZZ-1YY8N2-P PDT""r   )os_cryptbuiltinc                 `    t        | j                   r| j                  | j                         yy)NTF)r   
_test_hash_set_calc_checksum_backend_calc_checksum_os_cryptr   s    r   _load_backend_os_cryptz#_SHA2_Common._load_backend_os_crypti  s(    s~~&**3+F+FGr   c                    | j                         }t        ||      }|| j                  |      S | j                  }|j	                  | j
                        r|| dz
     t        k7  r!t        j                  j                  | ||      || d  S )Nr   )
r   r   _calc_checksum_builtinchecksum_sizer   r   r   rZ   r[   CryptBackendError)r   secretconfigr   css        r   r   z$_SHA2_Common._calc_checksum_os_cryptq  s    !&&)< ..v66 tzz*dB3q5kX.E&&**4>>RCDzr   c                 :    | j                  | j                         y)NT)r   r   r   s    r   _load_backend_builtinz"_SHA2_Common._load_backend_builtin  s    &&s'A'ABr   c                 Z    t        || j                  | j                  | j                        S N)r   rj   rk   _cdb_use_512)r   r   s     r   r   z#_SHA2_Common._calc_checksum_builtin  s&    vtyy$++#002 	2r   r   )__name__
__module____qualname____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   __classcell__)r   s   @r   r   r      s    L
 FL__N MJJJKLN
 O/DH * *X# 'H J "  2r   r   c                   0    e Zd ZdZd Z ed      ZdZdZdZ	y)r   aK  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/N)
r   r   r   r   namer	   r   r   default_roundsr   r   r   r   r   r     s)    *\ DeHEMN
:Jr   c                   4    e Zd ZdZd Z ed      ZdZdZdZ	dZ
y)r   aK  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.1H3eGmse6pgsOgDisWBGI5c7TZauS0N)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   