o
    5h(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permsrT   o/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/passlib/handlers/sha2_crypt.py
<listcomp>   s     z#_raw_sha2_crypt.<locals>.<listcomp>r0   )
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datadcblockstailrV   rW   pairsrT   rX   rZ   _raw_sha2_crypt8   sr   
!
$
$.  
r   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)rp   rq   implicit_rounds	salt_sizer#   rP   rQ   linearFNc                    s8   t t| jdi | |d u r| jo| jdk}|| _d S )N  rT   )superr   __init__use_defaultsrq   r   )selfr   kwds	__class__rT   rZ   r     s   
z_SHA2_Common.__init__c                 C      | j || jd u dS N)relaxed)
_norm_saltchecksum)r   rp   rT   rT   rZ   _parse_salt     z_SHA2_Common._parse_saltc                 C   r   r   )_norm_roundsr   )r   rq   rT   rT   rZ   _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   )rq   rp   r   r   )r   ident
startswithr`   ra   InvalidHashErrorrc   split_UDOLLAR_UROUNDSpop_UZEROZeroPaddedRoundsErrorintMalformedHashError)clsr   r   partsrq   r   rp   chkrT   rT   rZ   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)rq   r   r   r   rp   r   r	   )r   r   rT   rT   rZ   	to_stringT  s   z_SHA2_Common.to_string)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   rZ   _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`   ra   CryptBackendError)r   secretconfigr   csrT   rT   rZ   r   q  s   

z$_SHA2_Common._calc_checksum_os_cryptc                 C   s   |  | j dS )NT)r   r   r   rT   rT   rZ   _load_backend_builtin  s   z"_SHA2_Common._load_backend_builtinc                 C   s   t || j| j| jS N)r   rp   rq   _cdb_use_512)r   r   rT   rT   rZ   r     s   z#_SHA2_Common._calc_checksum_builtinr   )__name__
__module____qualname____doc__setting_kwdsr`   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__rT   rT   r   rZ   r      s4    
,

r   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   rZ   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   rZ   r     s    /)F)&r   rd   logging	getLoggerr   logpasslib.utilsr   r   r   r   passlib.utils.binaryr   passlib.utils.compatr   r   r	   r
   passlib.utils.handlersutilshandlersr`   __all__r_   rk   rh   rf   r   r   r   r   HasManyBackends	HasRoundsHasSaltGenericHandlerr   r   r   rT   rT   rT   rZ   <module>   s2    	

 B B