o
    gI                     @   s~  U d dl Z d dlmZ d dlmZ d dlmZmZ d dlm	Z	 e
ejZdZd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zereede dd Ze Ze  Ze! Ze" Ze# Ze$ Ze% Ze& Ze' Ze( Ze) Ze* Ze+ Ze, Ze- Z.e/e0d< e1 Z2e/e0d< e3 Z4e/e0d	< e5 Z6e/e0d
< e7 Z8e/e0d< e9 Z:e/e0d< e; Z<e/e0d< e= Z>e/e0d< e? Z@e/e0d< eedeA dd ZBeCe0d< eD ZEe/e0d< eF ZGe/e0d< eH ZIe/e0d< eJ ZKe/e0d< eL ZMe/e0d< eN ZOe/e0d< eP ZQe/e0d< eR ZSe/e0d< eT ZUe/e0d< eV ZWe/e0d< eedeX dd ZYeCe0d< eZ Z[e/e0d< e\ Z]e/e0d< e^ Z_e/e0d< e` Zae/e0d< eb Zce/e0d < ed Zee/e0d!< ef Zge/e0d"< eh Zie/e0d#< ej Zke/e0d$< el Zme/e0d%< eZneZoeZpeZqeZreZsd&Ztd'Zud(Zvd)Zwewfd*e/d+e/d,e/d-e/d.df
d/d0Zxd1e/d2e/d.ee/e/e/f fd3d4Zyd5ewfd6eCd7eCd*e/d+e/d,e/d8e/d-e/d.eCfd9d:Zzeneofd6eCd1e/d2e/d.eCfd;d<Z{d=eCd6eCd.e
fd>d?Z|d1e/d2e/d@e/d.dfdAdBZ}dCe/d6eCd7eCd1e/d2e/d@e/d.eCfdDdEZ~d6eCd1e/d2e/d@e/d.eCf
dFdGZd=eCd6eCd.e
fdHdIZeZdS )J    N)Tuple)ffilib)ensure    char *crypto_pwhash_ALG_ARGON2I13crypto_pwhash_ALG_ARGON2ID13crypto_pwhash_ALG_DEFAULTcrypto_pwhash_SALTBYTEScrypto_pwhash_STRBYTEScrypto_pwhash_PASSWD_MINcrypto_pwhash_PASSWD_MAXcrypto_pwhash_BYTES_MINcrypto_pwhash_BYTES_MAXcrypto_pwhash_argon2i_STRPREFIX"crypto_pwhash_argon2i_MEMLIMIT_MIN"crypto_pwhash_argon2i_MEMLIMIT_MAX"crypto_pwhash_argon2i_OPSLIMIT_MIN"crypto_pwhash_argon2i_OPSLIMIT_MAX*crypto_pwhash_argon2i_OPSLIMIT_INTERACTIVE*crypto_pwhash_argon2i_MEMLIMIT_INTERACTIVE'crypto_pwhash_argon2i_OPSLIMIT_MODERATE'crypto_pwhash_argon2i_MEMLIMIT_MODERATE(crypto_pwhash_argon2i_OPSLIMIT_SENSITIVE(crypto_pwhash_argon2i_MEMLIMIT_SENSITIVE crypto_pwhash_argon2id_STRPREFIX#crypto_pwhash_argon2id_MEMLIMIT_MIN#crypto_pwhash_argon2id_MEMLIMIT_MAX#crypto_pwhash_argon2id_OPSLIMIT_MIN#crypto_pwhash_argon2id_OPSLIMIT_MAX+crypto_pwhash_argon2id_OPSLIMIT_INTERACTIVE+crypto_pwhash_argon2id_MEMLIMIT_INTERACTIVE(crypto_pwhash_argon2id_OPSLIMIT_MODERATE(crypto_pwhash_argon2id_MEMLIMIT_MODERATE)crypto_pwhash_argon2id_OPSLIMIT_SENSITIVE)crypto_pwhash_argon2id_MEMLIMIT_SENSITIVE??   l    i   nrpmaxmemreturnc                 C   s  t |dkdtjd t |dkdtjd t | | d @ dkdtjd t | dkdtjd t |t| kdttjd t | dd	| > k tjd |d
 | }td
 }t | d || ktjd d| | d  d }t |t| ktjd t |tj| ktjd t || |kdtjd d S )Nr   zInvalid block sizeraisingzInvalid parallelization factor   z Cost factor must be a power of 2zCost factor must be at least 2zp*r is greater than {}                z7Memory limit would be exceeded with the choosen n, r, p)r   exc
ValueErrorSCRYPT_PR_MAXformat
UINT64_MAXsysmaxsize)r)   r*   r+   r,   BleniVlen r@   U/var/www/html/api-tag/env/lib/python3.10/site-packages/nacl/bindings/crypto_pwhash.py_check_memory_occupation   s2   


rB   opslimitmemlimitc                 C   s   | dk rd} d}| |d k r)d}| d|  }t ddD ]}d| |d kr' nqn*||d  }t ddD ]}d| |d kr@ nq4| d d|  }|d	krOd	}|| }|||fS )
z/Python implementation of libsodium's pickparamsi      r4   r0   r5   r(   r3   r2   r'   )range)rC   rD   r*   r+   maxnn_log2maxrpr@   r@   rA    nacl_bindings_pick_scrypt_params   s*   
rJ   @   passwdsaltdklenc           	      C   s   t tdtjd t t|ttd t t|ttd t t|ttd t t| ttd t t|ttd t|||| t	
d|}t| t| |t||||||	}t |dkdtjd t	t	d||dd S )a  
    Derive a cryptographic key using the ``passwd`` and ``salt``
    given as input.

    The work factor can be tuned by by picking different
    values for the parameters

    :param bytes passwd:
    :param bytes salt:
    :param bytes salt: *must* be *exactly* :py:const:`.SALTBYTES` long
    :param int dklen:
    :param int opslimit:
    :param int n:
    :param int r: block size,
    :param int p: the parallelism factor
    :param int maxmem: the maximum available memory available for scrypt's
                       operations
    :rtype: bytes
    :raises nacl.exceptions.UnavailableError: If called when using a
        minimal build of libsodium.
    Not available in minimal buildr.   z	uint8_t[]r   $Unexpected failure in key derivationr   N)r   &has_crypto_pwhash_scryptsalsa208sha256r6   UnavailableError
isinstanceint	TypeErrorbytesrB   r   newr   %crypto_pwhash_scryptsalsa208sha256_lllenRuntimeErrorbuffercast)	rL   rM   r)   r*   r+   rN   r,   bufretr@   r@   rA   rX   	  s*   rX   c                 C   sP   t tdtjd tdt}t|| t	| ||}t |dkdtj
d t|S )a  
    Derive a cryptographic key using the ``passwd`` and ``salt``
    given as input, returning a string representation which includes
    the salt and the tuning parameters.

    The returned string can be directly stored as a password hash.

    See :py:func:`.crypto_pwhash_scryptsalsa208sha256` for a short
    discussion about ``opslimit`` and ``memlimit`` values.

    :param bytes passwd:
    :param int opslimit:
    :param int memlimit:
    :return: serialized key hash, including salt and tuning parameters
    :rtype: bytes
    :raises nacl.exceptions.UnavailableError: If called when using a
        minimal build of libsodium.
    rO   r.   char[]r   z&Unexpected failure in password hashing)r   rQ   r6   rR   r   rW   SCRYPT_STRBYTESr   &crypto_pwhash_scryptsalsa208sha256_strrY   rZ   string)rL   rC   rD   r]   r^   r@   r@   rA   ra   E  s   
ra   passwd_hashc                 C   sV   t tdtjd t t| td kdtjd t| |t|}t |dkdtj	d dS )a9  
    Verifies the ``passwd`` against the ``passwd_hash`` that was generated.
    Returns True or False depending on the success

    :param passwd_hash: bytes
    :param passwd: bytes
    :rtype: boolean
    :raises nacl.exceptions.UnavailableError: If called when using a
        minimal build of libsodium.
    rO   r.   r0   zInvalid password hashr   Wrong passwordT)
r   rQ   r6   rR   rY   r`   r7   r   -crypto_pwhash_scryptsalsa208sha256_str_verifyInvalidkeyErrorrc   rL   r^   r@   r@   rA   re   q  s   
re   algc                 C   s   |t kr6|tk rtdt|tkrtdt| tk r(tdt| tkr4tdtd S |tkrl|t	k rFtdt	|t
krRtdt
| tk r^tdt| tkrjtdtd S td)Nz"memlimit must be at least {} bytesz!memlimit must be at most {} byteszopslimit must be at least {}zopslimit must be at most {}zUnsupported algorithm)r   r   r6   r7   r9   r   r   r   r	   r   r   r   r    rU   )rC   rD   rh   r@   r@   rA   _check_argon2_limits_alg  sj   
ri   outlenc              
   C   s  t t| ttjd t t|ttjd t t|ttjd t t|ttjd t t|ttjd t|tkr@td	t| t
k rLtd	t
| tkrXtd	tt||| td| }t|| |t|||||}t |dkdtjd t|| dd S )	a  
    Derive a raw cryptographic key using the ``passwd`` and the ``salt``
    given as input to the ``alg`` algorithm.

    :param outlen: the length of the derived key
    :type outlen: int
    :param passwd: The input password
    :type passwd: bytes
    :param salt:
    :type salt: bytes
    :param opslimit: computational cost
    :type opslimit: int
    :param memlimit: memory cost
    :type memlimit: int
    :param alg: algorithm identifier
    :type alg: int
    :return: derived key
    :rtype: bytes
    r.   z"salt must be exactly {} bytes longz*derived key must be at least {} bytes longz)derived key must be at most {} bytes longzunsigned char[]r   rP   N)r   rS   rT   r6   rU   rV   rY   r   r7   r9   r   r   ri   r   rW   r   crypto_pwhashrZ   r[   )rj   rL   rM   rC   rD   rh   outbufr^   r@   r@   rA   crypto_pwhash_alg  sD   rm   c                 C   s   t t|ttd t t|ttd t t| ttd t||| tdd}t	|| t
| |||}t |dkdtjd t|S )a  
    Derive a cryptographic key using the ``passwd`` given as input
    and a random salt, returning a string representation which
    includes the salt, the tuning parameters and the used algorithm.

    :param passwd: The input password
    :type passwd: bytes
    :param opslimit: computational cost
    :type opslimit: int
    :param memlimit: memory cost
    :type memlimit: int
    :param alg: The algorithm to use
    :type alg: int
    :return: serialized derived key and parameters
    :rtype: bytes
    r.   r_   r2   r   rP   )r   rS   rT   rU   rV   ri   r   rW   r   crypto_pwhash_str_algrY   r6   rZ   rb   )rL   rC   rD   rh   rl   r^   r@   r@   rA   rn     s   
rn   c                 C   sf   t t| ttd t t|ttd t t| dkdtjd t| |t|}t |dkdtj	d dS )a4  
    Verifies the ``passwd`` against a given password hash.

    Returns True on success, raises InvalidkeyError on failure
    :param passwd_hash: saved password hash
    :type passwd_hash: bytes
    :param passwd: password to be checked
    :type passwd: bytes
    :return: success
    :rtype: boolean
    r.      z#Hash must be at most 127 bytes longr   rd   T)
r   rS   rV   rU   rY   r6   r7   r   crypto_pwhash_str_verifyrf   rg   r@   r@   rA   rp   =  s   
rp   )r;   typingr   nacl.exceptions
exceptionsr6   nacl._sodiumr   r   r   bool-PYNACL_HAS_CRYPTO_PWHASH_SCRYPTSALSA208SHA256rQ   ,crypto_pwhash_scryptsalsa208sha256_STRPREFIX,crypto_pwhash_scryptsalsa208sha256_SALTBYTES+crypto_pwhash_scryptsalsa208sha256_STRBYTES-crypto_pwhash_scryptsalsa208sha256_PASSWD_MIN-crypto_pwhash_scryptsalsa208sha256_PASSWD_MAX,crypto_pwhash_scryptsalsa208sha256_BYTES_MIN,crypto_pwhash_scryptsalsa208sha256_BYTES_MAX/crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_MIN/crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_MAX/crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_MIN/crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_MAX7crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_INTERACTIVE7crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_INTERACTIVE5crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_SENSITIVE5crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_SENSITIVErb   r\   ,crypto_pwhash_scryptsalsa208sha256_strprefix,crypto_pwhash_scryptsalsa208sha256_saltbytes+crypto_pwhash_scryptsalsa208sha256_strbytes-crypto_pwhash_scryptsalsa208sha256_passwd_min-crypto_pwhash_scryptsalsa208sha256_passwd_max,crypto_pwhash_scryptsalsa208sha256_bytes_min,crypto_pwhash_scryptsalsa208sha256_bytes_max/crypto_pwhash_scryptsalsa208sha256_memlimit_min/crypto_pwhash_scryptsalsa208sha256_memlimit_max/crypto_pwhash_scryptsalsa208sha256_opslimit_min/crypto_pwhash_scryptsalsa208sha256_opslimit_max7crypto_pwhash_scryptsalsa208sha256_opslimit_interactive7crypto_pwhash_scryptsalsa208sha256_memlimit_interactive5crypto_pwhash_scryptsalsa208sha256_opslimit_sensitive5crypto_pwhash_scryptsalsa208sha256_memlimit_sensitivecrypto_pwhash_alg_argon2i13r   rT   __annotations__crypto_pwhash_alg_argon2id13r	   crypto_pwhash_alg_defaultr
   crypto_pwhash_saltbytesr   crypto_pwhash_strbytesr   crypto_pwhash_passwd_minr   crypto_pwhash_passwd_maxr   crypto_pwhash_bytes_minr   crypto_pwhash_bytes_maxr   crypto_pwhash_argon2i_strprefixr   rV   "crypto_pwhash_argon2i_memlimit_minr   "crypto_pwhash_argon2i_memlimit_maxr   "crypto_pwhash_argon2i_opslimit_minr   "crypto_pwhash_argon2i_opslimit_maxr   *crypto_pwhash_argon2i_opslimit_interactiver   *crypto_pwhash_argon2i_memlimit_interactiver   'crypto_pwhash_argon2i_opslimit_moderater   'crypto_pwhash_argon2i_memlimit_moderater   (crypto_pwhash_argon2i_opslimit_sensitiver   (crypto_pwhash_argon2i_memlimit_sensitiver    crypto_pwhash_argon2id_strprefixr   #crypto_pwhash_argon2id_memlimit_minr   #crypto_pwhash_argon2id_memlimit_maxr   #crypto_pwhash_argon2id_opslimit_minr   #crypto_pwhash_argon2id_opslimit_maxr    +crypto_pwhash_argon2id_opslimit_interactiver!   +crypto_pwhash_argon2id_memlimit_interactiver"   (crypto_pwhash_argon2id_opslimit_moderater#   (crypto_pwhash_argon2id_memlimit_moderater$   )crypto_pwhash_argon2id_opslimit_sensitiver%   )crypto_pwhash_argon2id_memlimit_sensitiver&   SCRYPT_OPSLIMIT_INTERACTIVESCRYPT_MEMLIMIT_INTERACTIVESCRYPT_OPSLIMIT_SENSITIVESCRYPT_MEMLIMIT_SENSITIVESCRYPT_SALTBYTESr`   r8   LOG2_UINT64_MAXr:   SCRYPT_MAX_MEMrB   rJ   rX   ra   re   ri   rm   rn   rp    crypto_pwhash_argon2i_str_verifyr@   r@   r@   rA   <module>   s  





















*
&
>
,
!9
G
+