a
    ø£Be   ã                   @  s®   d dl mZ d dlZd dlmZ ddlmZ ddlmZm	Z	m
Z
 ddlmZ ddlmZmZmZ dd	lmZ ejZejZejZejZejZd
dddœdd„ZG dd„ dƒZdS )é    )ÚannotationsN)ÚClassVaré   )ÚLiteral)Ú
ParametersÚ_check_typesÚextract_parameters)ÚInvalidHashError)ÚTypeÚhash_secretÚverify_secret)ÚRFC_9106_LOW_MEMORYzbytes | strÚstrÚbytes)ÚsÚencodingÚreturnc                 C  s   t | tƒr| S |  |¡S )zM
    Ensure *s* is a bytes string.  Encode using *encoding* if it isn't.
    )Ú
isinstancer   Úencode)r   r   © r   úZ/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/argon2/_password_hasher.pyÚ_ensure_bytes   s    
r   c                	   @  s*  e Zd ZU dZddgZded< ded< eeee	e
dejfdddddddd	œd
d„Zedd dœdd„ƒZeddœdd„ƒZeddœdd„ƒZeddœdd„ƒZeddœdd„ƒZeddœdd„ƒZeddœdd„ƒZddœdddd œd!d"„Zejejejd#œZd$ed%< ddd&d'œd(d)„Zdd*d+œd,d-„ZdS ).ÚPasswordHashera   
    High level class to hash passwords with sensible defaults.

    Uses Argon2\ **id** by default and always uses a random salt_ for hashing.
    But it can verify any type of Argon2 as long as the hash is correctly
    encoded.

    The reason for this being a class is both for convenience to carry
    parameters and to verify the parameters only *once*.  Any unnecessary
    slowdown when hashing is a tangible advantage for a brute force attacker.

    :param int time_cost: Defines the amount of computation realized and
        therefore the execution time, given in number of iterations.
    :param int memory_cost: Defines the memory usage, given in kibibytes_.
    :param int parallelism: Defines the number of parallel threads (*changes*
        the resulting hash value).
    :param int hash_len: Length of the hash in bytes.
    :param int salt_len: Length of random salt to be generated for each
        password.
    :param str encoding: The Argon2 C library expects bytes.  So if
        :meth:`hash` or :meth:`verify` are passed a ``str``, it will be
        encoded using this encoding.
    :param Type type: Argon2 type to use.  Only change for interoperability
        with legacy systems.

    .. versionadded:: 16.0.0
    .. versionchanged:: 18.2.0
       Switch from Argon2i to Argon2id based on the recommendation by the
       current RFC draft. See also :doc:`parameters`.
    .. versionchanged:: 18.2.0
       Changed default *memory_cost* to 100 MiB and default *parallelism* to 8.
    .. versionchanged:: 18.2.0 ``verify`` now will determine the type of hash.
    .. versionchanged:: 18.3.0 The Argon2 type is configurable now.
    .. versionadded:: 21.2.0 :meth:`from_parameters`
    .. versionchanged:: 21.2.0
       Changed defaults to :data:`argon2.profiles.RFC_9106_LOW_MEMORY`.

    .. _salt: https://en.wikipedia.org/wiki/Salt_(cryptography)
    .. _kibibytes: https://en.wikipedia.org/wiki/Binary_prefix#kibi
    Ú_parametersr   r   r   zutf-8Úintr
   ©Ú	time_costÚmemory_costÚparallelismÚhash_lenÚsalt_lenr   Útypec           	   	   C  s`   t |tf|tf|tf|tf|tf|tf|tfd�}|r>t|ƒ‚t|d|||||d�| _|| _d S )Nr   é   )r!   Úversionr    r   r   r   r   )r   r   r   r
   Ú	TypeErrorr   r   r   )	Úselfr   r   r   r   r    r   r!   Úer   r   r   Ú__init__N   s*    
ù	ù	zPasswordHasher.__init__)Úparamsr   c                 C  s   | ƒ }||_ |S )z_
        Construct a `PasswordHasher` from *params*.

        .. versionadded:: 21.2.0
        )r   )Úclsr(   Úphr   r   r   Úfrom_parametersp   s    zPasswordHasher.from_parameters)r   c                 C  s   | j jS ©N)r   r   ©r%   r   r   r   r   |   s    zPasswordHasher.time_costc                 C  s   | j jS r,   )r   r   r-   r   r   r   r   €   s    zPasswordHasher.memory_costc                 C  s   | j jS r,   )r   r   r-   r   r   r   r   „   s    zPasswordHasher.parallelismc                 C  s   | j jS r,   )r   r   r-   r   r   r   r   ˆ   s    zPasswordHasher.hash_lenc                 C  s   | j jS r,   )r   r    r-   r   r   r   r    Œ   s    zPasswordHasher.salt_lenc                 C  s   | j jS r,   )r   r!   r-   r   r   r   r!   �   s    zPasswordHasher.typeN)Úsaltzstr | byteszbytes | None)Úpasswordr.   r   c             	   C  s:   t t|| jƒ|pt | j¡| j| j| j| j	| j
d� d¡S )aÿ  
        Hash *password* and return an encoded hash.

        Parameters:

            password: Password to hash.

            salt: If None, a random salt is securely created.

                .. danger::

                    You should **not** pass a salt unless you really know what
                    you are doing.

        Raises:

            argon2.exceptions.HashingError: If hashing fails.

        Returns:

            Hashed *password*.

        .. versionadded:: 23.1.0 *salt* parameter
        )Úsecretr.   r   r   r   r   r!   Úascii)r   r   r   ÚosÚurandomr    r   r   r   r   r!   Údecode)r%   r/   r.   r   r   r   Úhash”   s    
ùøzPasswordHasher.hash)s	   $argon2i$s	   $argon2d$s	   $argon2idzClassVar[dict[bytes, Type]]Ú_header_to_typezLiteral[True])r5   r/   r   c                 C  sN   t |dƒ}z| j|dd…  }W n ty8   td‚Y n0 t|t || jƒ|ƒS )aÃ  
        Verify that *password* matches *hash*.

        .. warning::

            It is assumed that the caller is in full control of the hash.  No
            other parsing than the determination of the hash type is done by
            *argon2-cffi*.

        :param hash: An encoded hash as returned from
            :meth:`PasswordHasher.hash`.
        :type hash: ``bytes`` or ``str``

        :param password: The password to verify.
        :type password: ``bytes`` or ``str``

        :raises argon2.exceptions.VerifyMismatchError: If verification fails
            because *hash* is not valid for *password*.
        :raises argon2.exceptions.VerificationError: If verification fails for
            other reasons.
        :raises argon2.exceptions.InvalidHashError: If *hash* is so clearly
            invalid, that it couldn't be passed to Argon2.

        :return: ``True`` on success, raise
            :exc:`~argon2.exceptions.VerificationError` otherwise.
        :rtype: bool

        .. versionchanged:: 16.1.0
            Raise :exc:`~argon2.exceptions.VerifyMismatchError` on mismatches
            instead of its more generic superclass.
        .. versionadded:: 18.2.0 Hash type agility.
        r1   Né	   )r   r6   ÚLookupErrorr	   r   r   )r%   r5   r/   Z	hash_typer   r   r   Úverify½   s    #
ÿzPasswordHasher.verifyÚbool)r5   r   c                 C  s   | j t|ƒkS )aM  
        Check whether *hash* was created using the instance's parameters.

        Whenever your Argon2 parameters -- or *argon2-cffi*'s defaults! --
        change, you should rehash your passwords at the next opportunity.  The
        common approach is to do that whenever a user logs in, since that
        should be the only time when you have access to the cleartext
        password.

        Therefore it's best practice to check -- and if necessary rehash --
        passwords after each successful authentication.

        :rtype: bool

        .. versionadded:: 18.2.0
        )r   r   )r%   r5   r   r   r   Úcheck_needs_rehashê   s    z!PasswordHasher.check_needs_rehash)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú	__slots__Ú__annotations__ÚDEFAULT_TIME_COSTÚDEFAULT_MEMORY_COSTÚDEFAULT_PARALLELISMÚDEFAULT_HASH_LENGTHÚDEFAULT_RANDOM_SALT_LENGTHr
   ZIDr'   Úclassmethodr+   Úpropertyr   r   r   r   r    r!   r5   ÚIÚDr6   r9   r;   r   r   r   r   r       sB   
(ø"$ý-r   )Ú
__future__r   r2   Útypingr   Z_typingr   Ú_utilsr   r   r   Ú
exceptionsr	   Z	low_levelr
   r   r   Zprofilesr   r    rF   r   rE   r   rB   r   rC   r   rD   r   r   r   r   r   r   Ú<module>   s   	