U
    ΂dI                  
   @   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re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e/e/e/e/dd*d+d,Zxe/e/ee/e/e/f d-d.d/Zyd0ewfeCeCe/e/e/e/e/eCd1d2d3ZzeneofeCe/e/eCd4d5d6Z{eCeCe
d7d8d9Z|e/e/e/dd:d;d<Z}e/eCeCe/e/e/eCd=d>d?Z~eCe/e/e/eCd@dAdBZeCeCe
d7dCdDZeZdS )E    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Z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,   ZBleniZVlen r=   X/var/www/html/myproject/myenv/lib/python3.8/site-packages/nacl/bindings/crypto_pwhash.py_check_memory_occupation   s2    

r?   )opslimitmemlimitr-   c                 C   s   | dk rd} d}| |d k rRd}| d|  }t ddD ]}d| |d kr6 qq6nT||d  }t ddD ]}d| |d krh qqh| d d|  }|d	krd	}|| }|||fS )
z/Python implementation of libsodium's pickparamsi      r3   r/   r4   r(   r2   r1   r'   )range)r@   rA   r*   r+   ZmaxnZn_log2Zmaxrpr=   r=   r>    nacl_bindings_pick_scrypt_params   s$    rD   @   )passwdsaltr)   r*   r+   dklenr,   r-   c           	      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_scryptsalsa208sha256r5   UnavailableError
isinstanceint	TypeErrorbytesr?   r   newr   %crypto_pwhash_scryptsalsa208sha256_lllenRuntimeErrorbuffercast)	rF   rG   r)   r*   r+   rH   r,   bufretr=   r=   r>   rR   	  s:            rR   )rF   r@   rA   r-   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.
    rI   r.   char[]r   z&Unexpected failure in password hashing)r   rK   r5   rL   r   rQ   SCRYPT_STRBYTESr   &crypto_pwhash_scryptsalsa208sha256_strrS   rT   string)rF   r@   rA   rW   rX   r=   r=   r>   r[   E  s&        r[   )passwd_hashrF   r-   c                 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.
    rI   r.   r/   zInvalid password hashr   Wrong passwordT)
r   rK   r5   rL   rS   rZ   r6   r   -crypto_pwhash_scryptsalsa208sha256_str_verifyInvalidkeyErrorr]   rF   rX   r=   r=   r>   r_   q  s"      r_   )r@   rA   algr-   c                 C   s   |t krn|tk r"tdtn|tkr:tdt| tk rTtdtq| tkrtdtnx|tkr|t	k rtdt	n|t
krtdt
| tk rtdtq| tkrtdtn
tdd S )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   r5   r6   r8   r   r   r   r	   r   r   r   r    rO   )r@   rA   rb   r=   r=   r>   _check_argon2_limits_alg  sf    rc   )outlenrF   rG   r@   rA   rb   r-   c              
   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rtd	t| t
k rtd	t
n| tkrt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   rJ   N)r   rM   rN   r5   rO   rP   rS   r   r6   r8   r   r   rc   r   rQ   r   Zcrypto_pwhashrT   rU   )rd   rF   rG   r@   rA   rb   outbufrX   r=   r=   r>   crypto_pwhash_alg  sR           rf   )rF   r@   rA   rb   r-   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.   rY   r1   r   rJ   )r   rM   rN   rO   rP   rc   r   rQ   r   crypto_pwhash_str_algrS   r5   rT   r\   )rF   r@   rA   rb   re   rX   r=   r=   r>   rg     s&         rg   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   r^   T)
r   rM   rP   rO   rS   r5   r6   r   crypto_pwhash_str_verifyr`   ra   r=   r=   r>   ri   =  s    
ri   )r:   typingr   Znacl.exceptions
exceptionsr5   Znacl._sodiumr   r   r   boolZ-PYNACL_HAS_CRYPTO_PWHASH_SCRYPTSALSA208SHA256rK   Z,crypto_pwhash_scryptsalsa208sha256_STRPREFIXZ,crypto_pwhash_scryptsalsa208sha256_SALTBYTESZ+crypto_pwhash_scryptsalsa208sha256_STRBYTESZ-crypto_pwhash_scryptsalsa208sha256_PASSWD_MINZ-crypto_pwhash_scryptsalsa208sha256_PASSWD_MAXZ,crypto_pwhash_scryptsalsa208sha256_BYTES_MINZ,crypto_pwhash_scryptsalsa208sha256_BYTES_MAXZ/crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_MINZ/crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_MAXZ/crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_MINZ/crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_MAXZ7crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_INTERACTIVEZ7crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_INTERACTIVEZ5crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_SENSITIVEZ5crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_SENSITIVEr\   rV   Z,crypto_pwhash_scryptsalsa208sha256_strprefixZ,crypto_pwhash_scryptsalsa208sha256_saltbytesZ+crypto_pwhash_scryptsalsa208sha256_strbytesZ-crypto_pwhash_scryptsalsa208sha256_passwd_minZ-crypto_pwhash_scryptsalsa208sha256_passwd_maxZ,crypto_pwhash_scryptsalsa208sha256_bytes_minZ,crypto_pwhash_scryptsalsa208sha256_bytes_maxZ/crypto_pwhash_scryptsalsa208sha256_memlimit_minZ/crypto_pwhash_scryptsalsa208sha256_memlimit_maxZ/crypto_pwhash_scryptsalsa208sha256_opslimit_minZ/crypto_pwhash_scryptsalsa208sha256_opslimit_maxZ7crypto_pwhash_scryptsalsa208sha256_opslimit_interactiveZ7crypto_pwhash_scryptsalsa208sha256_memlimit_interactiveZ5crypto_pwhash_scryptsalsa208sha256_opslimit_sensitiveZ5crypto_pwhash_scryptsalsa208sha256_memlimit_sensitiveZcrypto_pwhash_alg_argon2i13r   rN   __annotations__Zcrypto_pwhash_alg_argon2id13r	   Zcrypto_pwhash_alg_defaultr
   Zcrypto_pwhash_saltbytesr   Zcrypto_pwhash_strbytesr   Zcrypto_pwhash_passwd_minr   Zcrypto_pwhash_passwd_maxr   Zcrypto_pwhash_bytes_minr   Zcrypto_pwhash_bytes_maxr   Zcrypto_pwhash_argon2i_strprefixr   rP   Z"crypto_pwhash_argon2i_memlimit_minr   Z"crypto_pwhash_argon2i_memlimit_maxr   Z"crypto_pwhash_argon2i_opslimit_minr   Z"crypto_pwhash_argon2i_opslimit_maxr   Z*crypto_pwhash_argon2i_opslimit_interactiver   Z*crypto_pwhash_argon2i_memlimit_interactiver   Z'crypto_pwhash_argon2i_opslimit_moderater   Z'crypto_pwhash_argon2i_memlimit_moderater   Z(crypto_pwhash_argon2i_opslimit_sensitiver   Z(crypto_pwhash_argon2i_memlimit_sensitiver   Z crypto_pwhash_argon2id_strprefixr   Z#crypto_pwhash_argon2id_memlimit_minr   Z#crypto_pwhash_argon2id_memlimit_maxr   Z#crypto_pwhash_argon2id_opslimit_minr   Z#crypto_pwhash_argon2id_opslimit_maxr    Z+crypto_pwhash_argon2id_opslimit_interactiver!   Z+crypto_pwhash_argon2id_memlimit_interactiver"   Z(crypto_pwhash_argon2id_opslimit_moderater#   Z(crypto_pwhash_argon2id_memlimit_moderater$   Z)crypto_pwhash_argon2id_opslimit_sensitiver%   Z)crypto_pwhash_argon2id_memlimit_sensitiver&   ZSCRYPT_OPSLIMIT_INTERACTIVEZSCRYPT_MEMLIMIT_INTERACTIVEZSCRYPT_OPSLIMIT_SENSITIVEZSCRYPT_MEMLIMIT_SENSITIVEZSCRYPT_SALTBYTESrZ   r7   ZLOG2_UINT64_MAXr9   ZSCRYPT_MAX_MEMr?   rD   rR   r[   r_   rc   rf   rg   ri   Z crypto_pwhash_argon2i_str_verifyr=   r=   r=   r>   <module>   sb   



















   + &>- !:H+