o
    5hBJ                     @   s  d Z ddlmZmZ ddlmZmZ ddlZee	Z
ddlmZ ddlmZmZ ddlmZmZmZmZ ddlmZ ddlm  mZ g d	ZG d
d dejejejejZ dde	fddZ!e!dddeddZ"e!dddZ#e!dddZ$ej%de"ddddZ&ej%de#ddddZ'ej%d e$d!d"ddZ(d#Z)G d$d% d%ejejejejZ*G d&d' d'ejej+ejZ,G d(d) d)ejejejZ-G d*d+ d+ejejejejZ.dS ),z,passlib.handlers.pbkdf - PBKDF2 based hashes    )hexlify	unhexlify)	b64encode	b64decodeN)
to_unicode)ab64_decodeab64_encode)str_to_basciiuuascii_to_strunicode)pbkdf2_hmac)pbkdf2_sha1pbkdf2_sha256pbkdf2_sha512cta_pbkdf2_sha1dlitz_pbkdf2_sha1grub_pbkdf2_sha512c                   @   sR   e Zd ZdZdZejZdZdZ	dZ
dZdZdZdZed	d
 Zdd Zdd ZdS )Pbkdf2DigestHandlerz1base class for various pbkdf2_{digest} algorithmssalt	salt_sizerounds      N       linearc                 C   sF   t j|| j| d\}}}t|d}|rt|d}| |||dS )N)handlerasciir   r   checksum)uh	parse_mc3identr   encodeclshashr   r   chk r*   k/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/passlib/handlers/pbkdf2.pyfrom_string?   s
   zPbkdf2DigestHandler.from_stringc                 C   s4   t | jd}t | jd}t| j| j||S Nr   )r   r   decoder!   r"   
render_mc3r$   r   selfr   r)   r*   r*   r+   	to_stringG   s   zPbkdf2DigestHandler.to_stringc                 C   s   t | j|| j| j| jS )N)r   _digestr   r   checksum_sizer1   secretr*   r*   r+   _calc_checksumL   s   z"Pbkdf2DigestHandler._calc_checksum)__name__
__module____qualname____doc__setting_kwdsr"   HASH64_CHARSchecksum_charsdefault_salt_sizemax_salt_sizedefault_rounds
min_rounds
max_roundsrounds_costr3   classmethodr,   r2   r7   r*   r*   r*   r+   r      s    
r   i.  c                 C   sd   d|  }|du rt d| f }t}t||ft|||| |||d d d dt|  |j|d d	S )
z;create new Pbkdf2DigestHandler subclass for a specific hashpbkdf2_Nz$pbkdf2-%s$         a$  This class implements a generic ``PBKDF2-HMAC-%(digest)s``-based 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: bytes
    :param salt:
        Optional salt bytes.
        If specified, the length must be between 0-1024 bytes.
        If not specified, a %(dsc)d byte salt will be autogenerated (this is recommended).

    :type salt_size: int
    :param salt_size:
        Optional number of bytes to use when autogenerating new salts.
        Defaults to %(dsc)d bytes, but can be any value between 0 and 1024.

    :type rounds: int
    :param rounds:
        Optional number of rounds to use.
        Defaults to %(dr)d, but must be within ``range(1,1<<32)``.

    :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
    )digestdscdr)r9   namer$   r3   rA   r4   encoded_checksum_sizer;   )r
   r   typedictupperr?   )	hash_namedigest_sizer   r$   modulerM   baser*   r*   r+   create_pbkdf2_hashP   s    
rV   sha1   i z$pbkdf2$)r$   sha256    iHq  sha512@   ia  ldap_pbkdf2_sha1z{PBKDF2}Tldap_pbkdf2_sha256z{PBKDF2-SHA256}z$pbkdf2-sha256$ldap_pbkdf2_sha512z{PBKDF2-SHA512}z$pbkdf2-sha512$s   -_c                   @   sZ   e Zd ZdZd ZdZedZdZdZ	dZ
ejZdZdZd	Zed
d Zdd Zdd ZdS )r   a  This class implements Cryptacular's PBKDF2-based crypt algorithm, 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: bytes
    :param salt:
        Optional salt bytes.
        If specified, it may be any length.
        If not specified, a one will be autogenerated (this is recommended).

    :type salt_size: int
    :param salt_size:
        Optional number of bytes to use when autogenerating new salts.
        Defaults to 16 bytes, but can be any value between 0 and 1024.

    :type rounds: int
    :param rounds:
        Optional number of rounds to use.
        Defaults to 60000, must be within ``range(1,1<<32)``.

    :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
    r   $p5k2$rX   r   r   r   r   r   c                 C   sL   t j|| jd| d\}}}t|dt}|rt|dt}| |||dS )Nr   )rounds_baser   r   r    )r"   r#   r$   r   r%   CTA_ALTCHARSr&   r*   r*   r+   r,      s
   zcta_pbkdf2_sha1.from_stringc                 C   s<   t | jtd}t | jtd}tj| j| j||ddS )Nr   r   ra   )	r   r   rb   r.   r!   r"   r/   r$   r   r0   r*   r*   r+   r2      s   zcta_pbkdf2_sha1.to_stringc                 C      t d|| j| jdS )NrW   rX   r   r   r   r5   r*   r*   r+   r7      s   zcta_pbkdf2_sha1._calc_checksumN)r8   r9   r:   r;   rM   r<   r
   r$   r4   r?   r@   r   rA   rB   rC   rD   rE   r,   r2   r7   r*   r*   r*   r+   r      s     %
r   c                   @   sl   e Zd ZdZd ZdZedZedZdZ	dZ
ejZejZdZdZd	Zed
d Zdd Zdd Zdd ZdS )r   aC  This class implements Dwayne Litzenberger's PBKDF2-based crypt algorithm, 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 specified, it may be any length, but must use the characters in the regexp range ``[./0-9A-Za-z]``.
        If not specified, a 16 character salt will be autogenerated (this is recommended).

    :type salt_size: int
    :param salt_size:
        Optional number of bytes to use when autogenerating new salts.
        Defaults to 16 bytes, but can be any value between 0 and 1024.

    :type rounds: int
    :param rounds:
        Optional number of rounds to use.
        Defaults to 60000, must be within ``range(1,1<<32)``.

    :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
    r   r`   z1000000000000000000000000000000000000000000000000=r   r   r   r   r   c                 C   s*   t j|| jdd| d\}}}| |||dS )Nr     )ra   rA   r   r    )r"   r#   r$   r&   r*   r*   r+   r,   9  s   zdlitz_pbkdf2_sha1.from_stringc                 C   s,   | j }|dkr	d }tj| j|| j| jddS Nrf   r   rc   )r   r"   r/   r$   r   r!   r1   r   r*   r*   r+   r2   ?  s   zdlitz_pbkdf2_sha1.to_stringc                 C   s*   | j }|dkr	d }tj| j|| jd ddS rg   )r   r"   r/   r$   r   rh   r*   r*   r+   _get_configE  s   zdlitz_pbkdf2_sha1._get_configc                 C   s(   |   }td||| jd}t|dS )NrW      r   )ri   r   r   r   r.   )r1   r6   r   resultr*   r*   r+   r7   N  s   z dlitz_pbkdf2_sha1._calc_checksumN)r8   r9   r:   r;   rM   r<   r
   r$   _stub_checksumr?   r@   r"   r=   
salt_charsr   rA   rB   rC   rD   rE   r,   r2   ri   r7   r*   r*   r*   r+   r      s$    %
	r   c                   @   sH   e Zd ZdZd ZdZedZdZd Z	Z
edd Zdd	 Zd
d ZdS )atlassian_pbkdf2_sha1aB  This class implements the PBKDF2 hash used by Atlassian.

    It supports a fixed-length salt, and a fixed number of rounds.

    The :meth:`~passlib.ifc.PasswordHash.using` method accepts the following optional keywords:

    :type salt: bytes
    :param salt:
        Optional salt bytes.
        If specified, the length must be exactly 16 bytes.
        If not specified, a salt will be autogenerated (this is recommended).

    :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
    )r   z	{PKCS5S2}rZ   r   c                 C   sh   t |dd}| j}||stj| t|t|d  d}|d d |dd  }}| ||dS )Nr   r(   r   )r   r!   )	r   r$   
startswithr"   excInvalidHashErrorr   lenr%   )r'   r(   r$   datar   r)   r*   r*   r+   r,   {  s   
z!atlassian_pbkdf2_sha1.from_stringc                 C   s(   | j | j }| jt|d }t|S r-   )r   r!   r$   r   r.   r   )r1   rs   r(   r*   r*   r+   r2     s   zatlassian_pbkdf2_sha1.to_stringc                 C   s   t d|| jddS )NrW   i'  rZ   )r   r   r5   r*   r*   r+   r7     s   z$atlassian_pbkdf2_sha1._calc_checksumN)r8   r9   r:   r;   rM   r<   r
   r$   r4   min_salt_sizer@   rE   r,   r2   r7   r*   r*   r*   r+   rn   [  s    
	rn   c                   @   sZ   e Zd ZdZd ZdZedZdZdZ	dZ
ejZdZdZdZed	d
 Zdd Zdd ZdS )r   a  This class implements Grub's pbkdf2-hmac-sha512 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: bytes
    :param salt:
        Optional salt bytes.
        If specified, the length must be between 0-1024 bytes.
        If not specified, a 64 byte salt will be autogenerated (this is recommended).

    :type salt_size: int
    :param salt_size:
        Optional number of bytes to use when autogenerating new salts.
        Defaults to 64 bytes, but can be any value between 0 and 1024.

    :type rounds: int
    :param rounds:
        Optional number of rounds to use.
        Defaults to 19000, but must be within ``range(1,1<<32)``.

    :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
    r   zgrub.pbkdf2.sha512.r\   r   r   r   r   c                 C   sL   t j|| jtd| d\}}}t|d}|rt|d}| |||dS )N.)sepr   r   r    )r"   r#   r$   r
   r   r%   r&   r*   r*   r+   r,     s   zgrub_pbkdf2_sha512.from_stringc                 C   sD   t | jd }t | jd }tj| j| j||t	ddS )Nr   ru   )rv   )
r   r   r.   rQ   r!   r"   r/   r$   r   r
   r0   r*   r*   r+   r2     s   zgrub_pbkdf2_sha512.to_stringc                 C   rd   )Nr[   r\   re   r5   r*   r*   r+   r7     s   z!grub_pbkdf2_sha512._calc_checksumN)r8   r9   r:   r;   rM   r<   r
   r$   r4   r?   r@   r   rA   rB   rC   rD   rE   r,   r2   r7   r*   r*   r*   r+   r     s      
r   )/r;   binasciir   r   base64r   r   logging	getLoggerr8   logpasslib.utilsr   passlib.utils.binaryr   r   passlib.utils.compatr	   r
   r   r   passlib.crypto.digestr   passlib.utils.handlersutilshandlersr"   __all__	HasRounds
HasRawSaltHasRawChecksumGenericHandlerr   rV   r   r   r   PrefixWrapperr]   r^   r_   rb   r   HasSaltr   rn   r   r*   r*   r*   r+   <module>   s.    33`i"8