o
    •Àvhr  ã                   @   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 |r“t|ƒ S S |r›t|ƒ 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   úP/var/www/html/hyperkenya/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/