U
    ;gr                     @   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 dd	d
gZe ZedkrddlmZmZmZmZmZ dd Zde_dd
 Zn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   s4  t |tsttdt|t |ts8ttdt|t |tsTttdt||dk rdtdt |tsttdt||dk rtd| tdd	d
dgkrttdt| t	j
t	jt	jt	jd|  }d}zJt|t	j}t|}t||t||t||||d	}t| t|W S |r.t| X dS )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
                )r   r   r   r   Nr   )
isinstancer   	TypeErrorr   r   r   
ValueErrorsetreprr   ZBCRYPT_SHA1_ALGORITHMZBCRYPT_SHA256_ALGORITHMZBCRYPT_SHA384_ALGORITHMZBCRYPT_SHA512_ALGORITHMr   r   ZBCRYPT_ALG_HANDLE_HMAC_FLAGr	   r   ZBCryptDeriveKeyPBKDF2lenr   r
   )	hash_algorithmpasswordsaltZ
iterationsZ
key_lengthZalg_constant
alg_handleZoutput_bufferres r'   6/tmp/pip-unpacked-wheel-x1gypflw/oscrypto/_win/util.pyr      sn    



	Fc                 C   s   t | tsttdt| | dk r,td| dkr<tdd}z6ttj	}t
| }t||| d}t| t|W S |rt| X dS )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   r   ZBCRYPT_RNG_ALGORITHMr	   r   ZBCryptGenRandomr   r
   )lengthr%   bufferr&   r'   r'   r(   r      s$    

)r   )r   N)
__future__r   r   r   r    r   _errorsr   _ffir	   r
   Z_pkcs12r   _typesr   r   r   __all__Z_backendZ_cngr   r   r   r   r   r   Zpure_pythonr   Z_pkcs5Z_randr'   r'   r'   r(   <module>   s"   j
/