o
    5h5                     @   s   d Z ddlmZ ddlZeeZddlmZm	Z	m
Z
 ddlmZ ddlmZmZ ddlm  mZ ddgZd	Zd
ZdZdZdZdddZG dd dejejZG dd dejeZG dd deZ dS )z0passlib.handlers.md5_crypt - md5-crypt algorithm    )md5N)
safe_crypt
test_cryptrepeat_string)h64)unicodeu	md5_cryptapr_md5_crypt    s   $1$s   $apr1$))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      Fc                    s  t | tr
| d} t | tsJ dt| v rtjtt	| }t |ts*J d|d}t	|dk s9J d|r>t
}nt}t| | |   }t| | | }|j}|t|| |}| dd }	|rv||d@ rmtn|	 |dL }|sf| }
| |  }| | }| ||||  ||  || g  fd	d
tD }|
}d}|r|D ]\}}t|t||    }q|d8 }|s|dd D ]\}}t|t||    }qt|tdS )a  perform raw md5-crypt calculation

    this function provides a pure-python implementation of the internals
    for the MD5-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 use_apr: use apache variant

    :returns:
        encoded checksum chars
    zutf-8zpwd not unicode or byteszsalt not unicodeasciir   zsalt too largeNr   c                    s    g | ]\}} |  | fqS  r   ).0evenoddpermsr   n/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/passlib/handlers/md5_crypt.py
<listcomp>   s     z"_raw_md5_crypt.<locals>.<listcomp>      )
isinstancer   encodebytes_BNULLuhexcNullPasswordErrorr	   len
_APR_MAGIC
_MD5_MAGICr   digestupdater   _c_digest_offsetsr   encode_transposed_bytes_transpose_mapdecode)pwdsaltuse_aprpwd_lenmagicdba_ctxa_ctx_updateievenchardapwd_pwdpwd_saltdatadcblocksr!   r"   r   r#   r%   _raw_md5_crypt,   sJ   


.rI   c                   @   s<   e Zd ZdZdZdZejZdZ	ejZ
edd Zdd Zd	S )
_MD5_Commonz+common code for md5_crypt and apr_md5_crypt)r:   	salt_size   r   c                 C   s"   t j|| j| d\}}| ||dS )N)handler)r:   checksum)r-   	parse_mc2ident)clshashr:   chkr   r   r%   from_string   s   z_MD5_Common.from_stringc                 C   s   t | j| j| jS N)r-   
render_mc2rP   r:   rN   )selfr   r   r%   	to_string   s   z_MD5_Common.to_stringN)__name__
__module____qualname____doc__setting_kwdschecksum_sizer-   HASH64_CHARSchecksum_charsmax_salt_size
salt_charsclassmethodrT   rX   r   r   r   r%   rJ      s    
rJ   c                   @   sH   e Zd ZdZd ZedZdZedd Z	dd Z
edd	 Zd
d ZdS )r	   a  This class implements the MD5-Crypt password hash, and follows the :ref:`password-hash-api`.

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

    :type salt_size: int
    :param salt_size:
        Optional number of characters to use when autogenerating new salts.
        Defaults to 8, but can be any value between 0 and 8.
        (This is mainly needed when generating Cisco-compatible hashes,
        which require ``salt_size=4``).

    :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
    z$1$)os_cryptbuiltinc                 C   s   t ddr| | j dS dS )Ntestz$1$test$pi/xDtU5WFVRqYS6BMU8X/TF)r   _set_calc_checksum_backend_calc_checksum_os_cryptrQ   r   r   r%   _load_backend_os_crypt  s   
z md5_crypt._load_backend_os_cryptc                 C   sb   | j | j }t||}|d u r| |S ||r#t|t|d kr+tj| |||dd  S )Nr'   i)	rP   r:   r   _calc_checksum_builtin
startswithr0   r-   r.   CryptBackendError)rW   secretconfigrR   r   r   r%   rh     s   

z!md5_crypt._calc_checksum_os_cryptc                 C   s   |  | j dS )NT)rg   rk   ri   r   r   r%   _load_backend_builtin%  s   zmd5_crypt._load_backend_builtinc                 C   s   t || jS rU   rI   r:   rW   rn   r   r   r%   rk   *  s   z md5_crypt._calc_checksum_builtinN)rY   rZ   r[   r\   namer   rP   backendsrc   rj   rh   rp   rk   r   r   r   r%   r	      s     

c                   @   s$   e Zd ZdZd ZedZdd ZdS )r
   ap  This class implements the Apr-MD5-Crypt password hash, and follows the :ref:`password-hash-api`.

    It supports a variable-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 0-8 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
    z$apr1$c                 C   s   t || jddS )NT)r;   rq   rr   r   r   r%   _calc_checksumQ  s   zapr_md5_crypt._calc_checksumN)rY   rZ   r[   r\   rs   r   rP   ru   r   r   r   r%   r
   1  s
    )F)!r\   hashlibr   logging	getLoggerrY   logpasslib.utilsr   r   r   passlib.utils.binaryr   passlib.utils.compatr   r   passlib.utils.handlersutilshandlersr-   __all__r,   r2   r1   r5   r7   rI   HasSaltGenericHandlerrJ   HasManyBackendsr	   r
   r   r   r   r%   <module>   s(    

  Q