o
    >er                     @   s   d dl mZmZmZm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 g dZe Zed	krVd
dlmZmZmZmZmZ dd Zde_dd ZdS ddlmZ ddlmZ dS )    )unicode_literalsdivisionabsolute_importprint_function   )backend)pretty_message)buffer_from_bytesbytes_from_buffer)
pkcs12_kdf)	type_namebyte_cls	int_types)pbkdf2r   
rand_byteswin   )bcryptBcryptConsthandle_erroropen_alg_handleclose_alg_handlec           	      C   s8  t |tsttdt|t |tsttdt|t |ts*ttdt||dk r2tdt |ts@ttdt||dk rHtd| tg dvrYttd	t| t	j
t	jt	jt	jd|  }d
}z+t|t	j}t|}t||t||t||||d	}t| t|W |rt| S S |rt| w w )a%  
        PBKDF2 from PKCS#5

        :param hash_algorithm:
            The string name of the hash algorithm to use: "sha1", "sha256", "sha384", "sha512"

        :param password:
            A byte string of the password to use an input to the KDF

        :param salt:
            A cryptographic random byte string

        :param iterations:
            The numbers of iterations to use when deriving the key

        :param key_length:
            The length of the desired key in bytes

        :raises:
            ValueError - when any of the parameters contain an invalid value
            TypeError - when any of the parameters are of the wrong type
            OSError - when an error is returned by the OS crypto library

        :return:
            The derived key as a byte string
        zH
                password must be a byte string, not %s
                zD
                salt must be a byte string, not %s
                zG
                iterations must be an integer, not %s
                r   z!iterations must be greater than 0zG
                key_length must be an integer, not %s
                z!key_length must be greater than 0)sha1sha256sha384sha512z|
                hash_algorithm must be one of "sha1", "sha256", "sha384", "sha512",
                not %s
                Nr   )
isinstancer   	TypeErrorr   r   r   
ValueErrorsetreprr   BCRYPT_SHA1_ALGORITHMBCRYPT_SHA256_ALGORITHMBCRYPT_SHA384_ALGORITHMBCRYPT_SHA512_ALGORITHMr   BCRYPT_ALG_HANDLE_HMAC_FLAGr	   r   BCryptDeriveKeyPBKDF2lenr   r
   r   )	hash_algorithmpasswordsalt
iterations
key_lengthalg_constant
alg_handleoutput_bufferres r1   S/var/www/html/humari/django-venv/lib/python3.10/site-packages/oscrypto/_win/util.pyr      st   



	

r   Fc                 C   s   t | tsttdt| | dk rtd| dkrtdd}z!ttj}t	| }t
||| d}t| t|W |rAt| S S |rIt| w w )a  
        Returns a number of random bytes suitable for cryptographic purposes

        :param length:
            The desired number of bytes

        :raises:
            ValueError - when any of the parameters contain an invalid value
            TypeError - when any of the parameters are of the wrong type
            OSError - when an error is returned by the OS crypto library

        :return:
            A byte string
        zC
                length must be an integer, not %s
                r   zlength must be greater than 0i   z$length must not be greater than 1024Nr   )r   r   r   r   r   r   r   r   BCRYPT_RNG_ALGORITHMr	   r   BCryptGenRandomr   r
   r   )lengthr.   bufferr0   r1   r1   r2   r      s*   



r   )r   )r   N)
__future__r   r   r   r    r   _errorsr   _ffir	   r
   _pkcs12r   _typesr   r   r   __all___backend_cngr   r   r   r   r   r   pure_pythonr   _pkcs5_randr1   r1   r1   r2   <module>   s   j/