B
    5‹dô  ã               @   sô   d Z ddlmZ ddlmZ ddlmZmZ ddlm	Z	 ddl
mZmZmZ dd	d
dddgZejZG dd	„ d	eƒZefeeeeeeeeedœ	dd„Zefeeeeeeeeedœ	dd„Zeeee	d dœdd„Zeeedœdd„Zeedœdd„ZdS )u@  
Low-level functions if you want to build your own higher level abstractions.

.. warning::
    This is a "Hazardous Materials" module.  You should **ONLY** use it if
    you're 100% absolutely sure that you know what youâ€™re doing because this
    module is full of land mines, dragons, and dinosaurs with laser guns.
é    )ÚEnum)ÚAny)ÚffiÚlibé   )ÚLiteral)ÚHashingErrorÚVerificationErrorÚVerifyMismatchErrorÚARGON2_VERSIONÚTyper   Úhash_secretÚhash_secret_rawÚverify_secretc               @   s"   e Zd ZdZejZejZej	Z
dS )r   zX
    Enum of Argon2 variants.

    Please see :doc:`parameters` on how to pick one.
    N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   ZArgon2_dÚDZArgon2_iÚIZ	Argon2_idZID© r   r   ú]/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/argon2/low_level.pyr   '   s
   )	ÚsecretÚsaltÚ	time_costÚmemory_costÚparallelismÚhash_lenÚtypeÚversionÚreturnc             C   sˆ   t  |||t|ƒ||j¡d }t d|¡}	t  |||t d| ¡t| ƒt d|¡t|ƒtj||	||j|¡}
|
t jkr~t	t
|
ƒƒ‚t |	¡S )aÜ  
    Hash *secret* and return an **encoded** hash.

    An encoded hash can be directly passed into :func:`verify_secret` as it
    contains all parameters and the salt.

    :param bytes secret: Secret to hash.
    :param bytes salt: A salt_.  Should be random and different for each
        secret.
    :param Type type: Which Argon2 variant to use.
    :param int version: Which Argon2 version to use.

    For an explanation of the Argon2 parameters see :class:`PasswordHasher`.

    :rtype: bytes

    :raises argon2.exceptions.HashingError: If hashing fails.

    .. versionadded:: 16.0.0

    .. _salt: https://en.wikipedia.org/wiki/Salt_(cryptography)
    .. _kibibytes: https://en.wikipedia.org/wiki/Binary_prefix#kibi
    r   zchar[]z	uint8_t[])r   Zargon2_encodedlenÚlenÚvaluer   ÚnewÚargon2_hashÚNULLÚ	ARGON2_OKr   Úerror_to_strÚstring)r   r   r   r   r   r   r   r   ÚsizeÚbufÚrvr   r   r   r   G   s4    "


c       
      C   sp   t  d|¡}t |||t  d| ¡t| ƒt  d|¡t|ƒ||t jd|j|¡}	|	tjkr`tt	|	ƒƒ‚t
t  ||¡ƒS )z—
    Hash *password* and return a **raw** hash.

    This function takes the same parameters as :func:`hash_secret`.

    .. versionadded:: 16.0.0
    z	uint8_t[]r   )r   r#   r   r$   r!   r%   r"   r&   r   r'   ÚbytesÚbuffer)
r   r   r   r   r   r   r   r   r*   r+   r   r   r   r   ‰   s$    


T)Úhashr   r   r    c             C   s\   t  t d| ¡t d|¡t|ƒ|j¡}|t jkr4dS |t jkrLtt	|ƒƒ‚nt
t	|ƒƒ‚dS )a8  
    Verify whether *secret* is correct for *hash* of *type*.

    :param bytes hash: An encoded Argon2 hash as returned by
        :func:`hash_secret`.
    :param bytes secret: The secret to verify whether it matches the one
        in *hash*.
    :param Type type: Type for *hash*.

    :raises argon2.exceptions.VerifyMismatchError: If verification fails
        because *hash* is not valid for *secret* of *type*.
    :raises argon2.exceptions.VerificationError: If verification fails for
        other reasons.

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

    .. versionadded:: 16.0.0
    .. versionchanged:: 16.1.0
        Raise :exc:`~argon2.exceptions.VerifyMismatchError` on mismatches
        instead of its more generic superclass.
    zchar[]z	uint8_t[]TN)r   Zargon2_verifyr   r#   r!   r"   r&   ZARGON2_VERIFY_MISMATCHr
   r'   r	   )r.   r   r   r+   r   r   r   r   ±   s    



)Úcontextr   r    c             C   s   t  | |¡S )a“  
    Direct binding to the ``argon2_ctx`` function.

    .. warning::
        This is a strictly advanced function working on raw C data structures.
        Both *Argon2*'s and *argon2-cffi*'s higher-level bindings do a lot of
        sanity checks and housekeeping work that *you* are now responsible for
        (e.g. clearing buffers). The structure of the *context* object can,
        has, and will change with *any* release!

        Use at your own peril; *argon2-cffi* does *not* use this binding
        itself.

    :param context: A CFFI *Argon2* context object (i.e. an ``struct
        Argon2_Context``/``argon2_context``).
    :param int type: Which *Argon2* variant to use.  You can use the ``value``
        field of :class:`Type`'s fields.

    :rtype: int
    :return: An *Argon2* error code.  Can be transformed into a string using
        :func:`error_to_str`.

    .. versionadded:: 16.0.0
    )r   Z
argon2_ctx)r/   r   r   r   r   Úcore×   s    r0   )Úerrorr    c             C   s   t  t | ¡¡}| d¡}|S )z´
    Convert an Argon2 error code into a native string.

    :param int error: An Argon2 error code as returned by :func:`core`.

    :rtype: str

    .. versionadded:: 16.0.0
    Úascii)r   r(   r   Zargon2_error_messageÚdecode)r1   Úmsgr   r   r   r'   ó   s    

r'   N)r   Úenumr   Útypingr   Z_argon2_cffi_bindingsr   r   Z_typingr   Ú
exceptionsr   r	   r
   Ú__all__ZARGON2_VERSION_NUMBERr   r   r,   Úintr   r   r   r0   Ústrr'   r   r   r   r   Ú<module>
   s(   	(A&