
    Xh"                       d dl mZ d dl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ZmZmZ ddlmZ  e       Zej(                  Zej,                  Zej0                  Zej4                  Zej8                  Zdd	Z G d
 d      Zy)    )annotationsN)ClassVarLiteral   )
Parameters_check_typesextract_parametersvalidate_params_for_platform)InvalidHashError)Typehash_secretverify_secret)get_default_parametersc                H    t        | t              r| S | j                  |      S )zM
    Ensure *s* is a bytes string.  Encode using *encoding* if it isn't.
    )
isinstancebytesencode)sencodings     K/var/www/html/myenv/lib/python3.12/site-packages/argon2/_password_hasher.py_ensure_bytesr      s"     !U88H    c                  n   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Zedd       Zedd	       Zedd
       Zedd       Zedd       Zedd       Zedd       ZddddZej0                  ej2                  ej                  dZded<   	 	 	 	 	 	 ddZddZy)PasswordHashera  
    High level class to hash passwords with sensible defaults.

    Uses Argon2\ **id** by default and 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.

    Args:
        time_cost:
            Defines the amount of computation realized and therefore the
            execution time, given in number of iterations.

        memory_cost: Defines the memory usage, given in kibibytes_.

        parallelism:
            Defines the number of parallel threads (*changes* the resulting
            hash value).

        hash_len: Length of the hash in bytes.

        salt_len: Length of random salt to be generated for each password.

        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.

        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   strzutf-8c           
         t        |t        f|t        f|t        f|t        f|t        f|t        f|t        f      }|rt	        |      t        |d|||||      }	t        |	       |	| _        || _        y )N)	time_costmemory_costparallelismhash_lensalt_lenr   type   )r#   versionr"   r!   r   r   r    )	r   intr   r   	TypeErrorr   r
   r   r   )
selfr   r   r    r!   r"   r   r#   eparamss
             r   __init__zPasswordHasher.__init__^   s      #&$c*$c*___
 A,##
 	%V, " r   c                     | |j                   |j                  |j                  |j                  |j                  |j
                        S )z
        Construct a `PasswordHasher` from *params*.

        Returns:
            A `PasswordHasher` instance with the parameters from *params*.

        .. versionadded:: 21.2.0
        r   r   r    r!   r"   r#   r-   )clsr*   s     r   from_parameterszPasswordHasher.from_parameters   sA     &&****____
 	
r   c                .    | j                   j                  S N)r   r   r(   s    r   r   zPasswordHasher.time_cost   s    )))r   c                .    | j                   j                  S r1   )r   r   r2   s    r   r   zPasswordHasher.memory_cost       +++r   c                .    | j                   j                  S r1   )r   r    r2   s    r   r    zPasswordHasher.parallelism   r4   r   c                .    | j                   j                  S r1   )r   r!   r2   s    r   r!   zPasswordHasher.hash_len       (((r   c                .    | j                   j                  S r1   )r   r"   r2   s    r   r"   zPasswordHasher.salt_len   r7   r   c                .    | j                   j                  S r1   )r   r#   r2   s    r   r#   zPasswordHasher.type   s    $$$r   N)saltc          	        t        t        || j                        |xs t        j                  | j
                        | j                  | j                  | j                  | j                  | j                        j                  d      S )a  
        Hash *password* and return an encoded hash.

        Args:
            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(   passwordr:   s      r   hashzPasswordHasher.hash   sf    .  4==92DMM2nn((((]]
 &/	r   )s	   $argon2i$s	   $argon2d$s	   $argon2idzClassVar[dict[bytes, Type]]_header_to_typec                    t        |d      }	 | j                  |dd    }t	        |t        || j
                        |      S # t        $ r t        dw xY w)ak  
        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*.

        Args:
            hash: An encoded hash as returned from :meth:`PasswordHasher.hash`.

            password: The password to verify.

        Raises:
            argon2.exceptions.VerifyMismatchError:
                If verification fails because *hash* is not valid for
                *password*.

            argon2.exceptions.VerificationError:
                If verification fails for other reasons.

            argon2.exceptions.InvalidHashError:
                If *hash* is so clearly invalid, that it couldn't be passed to
                Argon2.

        Returns:
            ``True`` on success, otherwise an exception is raised.

        .. versionchanged:: 16.1.0
            Raise :exc:`~argon2.exceptions.VerifyMismatchError` on mismatches
            instead of its more generic superclass.
        .. versionadded:: 18.2.0 Hash type agility.
        r=   N	   )r   rC   LookupErrorr   r   r   )r(   rB   rA   	hash_types       r   verifyzPasswordHasher.verify   sf    J T7+	-,,T"1X6I -$--8)
 	
  	-",	-s   A Ac                t    t        |t              r|j                  d      }| j                  t	        |      k7  S )a  
        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.

        Args:
            hash: An encoded Argon2 password hash.

        Returns:
            Whether *hash* was created using the instance's parameters.

        .. versionadded:: 18.2.0
        .. versionchanged:: 24.1.0 Accepts bytes for *hash*.
        r=   )r   r   r@   r   r	   )r(   rB   s     r   check_needs_rehashz!PasswordHasher.check_needs_rehash  s3    , dE";;w'D#5d#;;;r   )r   r&   r   r&   r    r&   r!   r&   r"   r&   r   r   r#   r   )r*   r   returnr   )rK   r&   )rK   r   )rA   str | bytesr:   zbytes | NonerK   r   )rB   rL   rA   rL   rK   zLiteral[True])rB   rL   rK   bool)__name__
__module____qualname____doc__	__slots____annotations__DEFAULT_TIME_COSTDEFAULT_MEMORY_COSTDEFAULT_PARALLELISMDEFAULT_HASH_LENGTHDEFAULT_RANDOM_SALT_LENGTHr   IDr+   classmethodr/   propertyr   r   r    r!   r"   r#   rB   IDrC   rH   rJ    r   r   r   r   &   sf   0d 
+IM +..+2WW$!$! $! 	$!
 $! $! $! $!L 
 
& * * , , , , ) ) ) ) % % CG D ffffgg4O0 -
-
+6-
	-
^<r   r   )r   zbytes | strr   r   rK   r   ) 
__future__r   r>   typingr   r   _utilsr   r   r	   r
   
exceptionsr   	low_levelr   r   r   profilesr   default_paramsr"   rX   r!   rW   r   rT   r   rU   r    rV   r   r   r^   r   r   <module>rf      s    # 	 $  ) 7 7 , ()+44 $-- ",, $00 $00 y< y<r   