o
    _6d(U                     @   s   d Z ddlZddlZe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  mZ ddgZdZd	Zd
ZdZdddZedZedZedZG dd dejej ej!ej"Z#G dd de#Z$G dd de#Z%dS )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   r/       r   5   6   !   r   r   7   "   #   r   8   9   $   r!   r#   :   %   &   r%   ;   <   '   r*   r,   =   (   )   r   >   ?   Fc                    s  t | tr
| d} t | tsJ t| v r tj|rtt	t
| }d|  kr1dks6J d J dt |ts?J d|d}t
|}|dk sPJ d|rXtj}t}ntj}t}|| | |   }|| | }	|	j}
|
t|| |}|r|
|d	@ r|n|  |d	L }|s{|	 }|d
k rt|| |  |}n|| }|j}|d	 }|r||  |d	8 }|st| |}t
||ksJ ||dt|d    d| }t
||ksJ d|| }|| }||||| || || g  fddtD }|}t|d\}}|r)|D ]\}}|||||    }q|d	8 }|s|r[|d	? }|d| D ]\}}|||||    }q6|d	@ r[|||| d   }t||dS )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!c                    s    g | ]\}} |  | fqS  rT   ).0evenoddZpermsrT   rC:\Users\jesus\OneDrive\Desktop\erpjis_fastapi\backend\jisbackend\Lib\site-packages\passlib/handlers/sha2_crypt.py
<listcomp>   s     z#_raw_sha2_crypt.<locals>.<listcomp>r0   )
isinstancer
   encodebytes_BNULLuhexcZNullPasswordErrorr   r   lenhashlibsha512_512_transpose_mapsha256_256_transpose_mapdigestupdater   r   _c_digest_offsetsdivmodr   Zencode_transposed_bytesdecode)pwdsaltroundsZuse_512Zpwd_lenZsalt_lenZ
hash_constZtranspose_mapdbZa_ctxZa_ctx_updateidaZdpZtmp_ctxZtmp_ctx_updateZdsZdp_dpZdp_dsdatadcblockstailrV   rW   pairsrT   rX   rY   _raw_sha2_crypt8   sr   
!
$
$.  
rw   zrounds=$0c                       s   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	d
	Zdd Zdd Zedd Zdd ZdZdZedd Zdd Zedd Zdd Z  ZS )_SHA2_CommonzBclass containing common code shared by sha256_crypt & sha512_crypt)rm   rn   implicit_roundsZ	salt_sizer#   rP   rQ   ZlinearFNc                    s8   t t| jdi | |d u r| jo| jdk}|| _d S )N  rT   )superrz   __init__Zuse_defaultsrn   r{   )selfr{   kwds	__class__rT   rY   r~     s   
z_SHA2_Common.__init__c                 C      | j || jd u dS N)relaxed)Z
_norm_saltchecksum)r   rm   rT   rT   rY   _parse_salt     z_SHA2_Common._parse_saltc                 C   r   r   )Z_norm_roundsr   )r   rn   rT   rT   rY   _parse_rounds#  r   z_SHA2_Common._parse_roundsc                 C   s   t |dd}| j}||stj| t|dksJ |dd  t}|d t	rStt	dks4J |
ddd  }|trL|tkrLtj| t|}d}nd}d}t|d	krb|\}}nt|d
kro|d }d }ntj| | |||p{d |dS )NrR   hashr   r   r&   Fr|   Tr   r   )rn   rm   r   r{   )r   ident
startswithr_   r`   ZInvalidHashErrorra   split_UDOLLAR_UROUNDSpop_UZEROZZeroPaddedRoundsErrorintZMalformedHashError)clsr   r   partsrn   r{   rm   ZchkrT   rT   rY   from_string'  s6   	

z_SHA2_Common.from_stringc                 C   sd   | j dkr| jrtd| j| j| jptdf }t|S td| j| j | j| jp+tdf }t|S )Nr|   z%s%s$%s z%srounds=%d$%s$%s)rn   r{   r   r   rm   r   r	   )r   r   rT   rT   rY   	to_stringT  s   z_SHA2_Common.to_string)Zos_cryptbuiltinc                 C   s   t | j r| | j dS dS )NTF)r   
_test_hash_set_calc_checksum_backend_calc_checksum_os_cryptr   rT   rT   rY   _load_backend_os_crypti  s   
z#_SHA2_Common._load_backend_os_cryptc                 C   sf   |   }t||}|d u r| |S | j}|| jr$|| d  tkr,tj	| |||| d  S )Nr   )
r   r   _calc_checksum_builtinchecksum_sizer   r   r   r_   r`   ZCryptBackendError)r   secretconfigr   csrT   rT   rY   r   q  s   

z$_SHA2_Common._calc_checksum_os_cryptc                 C   s   |  | j dS )NT)r   r   r   rT   rT   rY   _load_backend_builtin  s   z"_SHA2_Common._load_backend_builtinc                 C   s   t || j| j| jS N)rw   rm   rn   _cdb_use_512)r   r   rT   rT   rY   r     s   z#_SHA2_Common._calc_checksum_builtinr   )__name__
__module____qualname____doc__Zsetting_kwdsr_   ZHASH64_CHARSZchecksum_charsZmax_salt_sizeZ
salt_charsZ
min_rounds
max_roundsZrounds_costr   Z_rounds_prefixr{   r~   r   r   classmethodr   r   backendsr   r   r   r   r   __classcell__rT   rT   r   rY   rz      s4    
,

rz   c                   @   s(   e Zd ZdZd ZedZdZdZdZ	dS )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$r1   i) )testz?$5$rounds=1000$test$QmQADEXMG8POI5WDsaeho0P36yK3Tcrgboabng6bkb/N)
r   r   r   r   namer   r   r   default_roundsr   rT   rT   rT   rY   r     s    .c                   @   s,   e Zd ZdZd ZedZdZdZdZ	dZ
dS )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   rT   rT   rT   rY   r     s    /)F)&r   rb   logging	getLoggerr   logZpasslib.utilsr   r   r   r   Zpasslib.utils.binaryr   Zpasslib.utils.compatr   r   r	   r
   Zpasslib.utils.handlersutilshandlersr_   __all__r^   ri   rf   rd   rw   r   r   r   ZHasManyBackendsZ	HasRoundsZHasSaltZGenericHandlerrz   r   r   rT   rT   rT   rY   <module>   s2    	

 B B