
    Ujp                     L    d dl ZddlmZ ddlmZ ddlmZ  G d d          ZdS )    N   )
exceptions)HasherProtocol)validate_str_or_bytesc            	           e Zd ZdZdej        j        e         ddfdZe	dd            Z
dddeez  d	edz  defd
Zdeez  deez  defdZdeez  deez  deeedz  f         fdZdS )PasswordHashz0
    Represents a password hashing utility.
    hashersreturnNc                 h    t          |          dk    s
J d            || _        |d         | _        dS )z
        Args:
            hashers: A sequence of hashers to be used for password hashing.

        Raises:
            AssertionError: If no hashers are specified.
        r   z%You must specify at least one hasher.N)lenr	   current_hasher)selfr	   s     P/var/www/app.qaans.com/backend/venv/lib/python3.11/site-packages/pwdlib/_hash.py__init__zPasswordHash.__init__   s=     7||a!H%aj    c                 6    ddl m}  |  |            f          S )ag  
        Returns a PasswordHash instance with recommended hashers.

        Currently, the hasher is Argon2 with default parameters.

        Examples:
            >>> password_hash = PasswordHash.recommended()
            >>> hash = password_hash.hash("herminetincture")
            >>> password_hash.verify(hash, "herminetincture")
            True
        r   )Argon2Hasher)hashers.argon2r   )clsr   s     r   recommendedzPasswordHash.recommended   s/     	100000sLLNN$%%%r   saltpasswordr   c                Z    t          |d           | j                            ||          S )aE  
        Hashes a password using the current hasher.

        Args:
            password: The password to be hashed.
            salt: The salt to be used for hashing. Defaults to None.

        Returns:
            The hashed password.

        Examples:
            >>> hash = password_hash.hash("herminetincture")
        r   r   )r   r   hash)r   r   r   s      r   r   zPasswordHash.hash*   s0     	h
333"''t'<<<r   r   c                     t          |d           t          |d           | j        D ]/}|                    |          r|                    ||          c S 0t	          j        |          )a#  
        Verifies if a password matches a given hash.

        Args:
            password: The password to be checked.
            hash: The hash to be verified.

        Returns:
            True if the password matches the hash, False otherwise.

        Raises:
            exceptions.UnknownHashError: If the hash is not recognized by any of the hashers.

        Examples:
            >>> password_hash.verify("herminetincture", hash)
            True

            >>> password_hash.verify("INVALID_PASSWORD", hash)
            False
        r   r   )r   r	   identifyverifyr   UnknownHashError)r   r   r   hashers       r   r   zPasswordHash.verify;   sy    * 	h
333dF+++l 	5 	5Ft$$ 5}}Xt444445)$///r   c                 ^   t          |d           t          |d           | j        D ]r}|                    |          r[|                    ||          s dS d}|| j        k    s|                    |          r| j                            |          }d|fc S st          j        |          )aw  
        Verifies if a password matches a given hash and updates the hash if necessary.

        Args:
            password: The password to be checked.
            hash: The hash to be verified.

        Returns:
            A tuple containing a boolean indicating if the password matches the hash,
                and an updated hash if the current hasher or the hash itself needs to be updated.

        Raises:
            exceptions.UnknownHashError: If the hash is not recognized by any of the hashers.

        Examples:
            >>> valid, updated_hash = password_hash.verify_and_update("herminetincture", hash)
        r   r   )FNNT)	r   r	   r   r   r   check_needs_rehashr   r   r   )r   r   r   r    updated_hashs        r   verify_and_updatezPasswordHash.verify_and_updateW   s    ( 	h
333dF+++l 	. 	.Ft$$ .}}Xt44 .&;;/3L!4448Q8QRV8W8W4'+':'?'?'I'I----. )$///r   )r
   r   )__name__
__module____qualname____doc__collectionsabcSequencer   r   classmethodr   strbytesr   boolr   tupler$    r   r   r   r      s        
) 8 H 
)T 
) 
) 
) 
) & & & [&  CG = = =S5[ =54< =3 = = = ="0sU{ 0#+ 0$ 0 0 0 080e0+.;0	tS4Z	 0 0 0 0 0 0r   r   )	collections.abcr)    r   r	   r   hashers.baser   r   r1   r   r   <module>r5      s              # # # # # # / / / / / /n0 n0 n0 n0 n0 n0 n0 n0 n0 n0r   