o
    _6dBJ                     @   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saltZ	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)   nC:\Users\jesus\OneDrive\Desktop\erpjis_fastapi\backend\jisbackend\Lib\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r0   secretr)   r)   r*   _calc_checksumL   s   z"Pbkdf2DigestHandler._calc_checksum)__name__
__module____qualname____doc__setting_kwdsr!   HASH64_CHARSZchecksum_charsdefault_salt_sizemax_salt_sizedefault_rounds
min_rounds
max_roundsrounds_costr2   classmethodr+   r1   r6   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 hashZ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
    )digestZdscZdr)r8   namer#   r2   r?   r3   Zencoded_checksum_sizer:   )r
   r   typedictupperr=   )	hash_namedigest_sizer   r#   modulerH   baser)   r)   r*   create_pbkdf2_hashP   s    
rP   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$rR   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   r[   )	r   r   r\   r-   r    r!   r.   r#   r   r/   r)   r)   r*   r1      s   zcta_pbkdf2_sha1.to_stringc                 C      t d|| j| jdS )NrQ   rR   r   r   r   r4   r)   r)   r*   r6      s   zcta_pbkdf2_sha1._calc_checksumN)r7   r8   r9   r:   rH   r;   r
   r#   r3   r=   r>   r   r?   r@   rA   rB   rC   r+   r1   r6   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   rZ   z1000000000000000000000000000000000000000000000000=r   r   r   r   r   c                 C   s*   t j|| jdd| d\}}}| |||dS )Nr     )r[   r?   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 Nr`   r   r]   )r   r!   r.   r#   r   r    r0   r   r)   r)   r*   r1   ?  s   zdlitz_pbkdf2_sha1.to_stringc                 C   s*   | j }|dkr	d }tj| j|| jd ddS ra   )r   r!   r.   r#   r   rb   r)   r)   r*   _get_configE  s   zdlitz_pbkdf2_sha1._get_configc                 C   s(   |   }td||| jd}t|dS )NrQ      r   )rc   r   r   r   r-   )r0   r5   r   resultr)   r)   r*   r6   N  s   z dlitz_pbkdf2_sha1._calc_checksumN)r7   r8   r9   r:   rH   r;   r
   r#   Z_stub_checksumr=   r>   r!   r<   Z
salt_charsr   r?   r@   rA   rB   rC   r+   r1   rc   r6   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}rT   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!   excZ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   )r0   rj   r'   r)   r)   r*   r1     s   zatlassian_pbkdf2_sha1.to_stringc                 C   s   t d|| jddS )NrQ   i'  rT   )r   r   r4   r)   r)   r*   r6     s   z$atlassian_pbkdf2_sha1._calc_checksumN)r7   r8   r9   r:   rH   r;   r
   r#   r3   Zmin_salt_sizer>   rC   r+   r1   r6   r)   r)   r)   r*   rf   [  s    
	rf   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.rV   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   rk   )rl   )
r   r   r-   rK   r    r!   r.   r#   r   r
   r/   r)   r)   r*   r1     s   zgrub_pbkdf2_sha512.to_stringc                 C   r^   )NrU   rV   r_   r4   r)   r)   r*   r6     s   z!grub_pbkdf2_sha512._calc_checksumN)r7   r8   r9   r:   rH   r;   r
   r#   r3   r=   r>   r   r?   r@   rA   rB   rC   r+   r1   r6   r)   r)   r)   r*   r     s      
r   )/r:   binasciir   r   base64r   r   logging	getLoggerr7   logZpasslib.utilsr   Zpasslib.utils.binaryr   r   Zpasslib.utils.compatr	   r
   r   r   Zpasslib.crypto.digestr   Zpasslib.utils.handlersutilshandlersr!   __all__Z	HasRoundsZ
HasRawSaltZHasRawChecksumZGenericHandlerr   rP   r   r   r   ZPrefixWrapperrW   rX   rY   r\   r   ZHasSaltr   rf   r   r)   r)   r)   r*   <module>   s.    33`i"8