o
    &zhæ  ã                   @   s    d dl mZmZmZmZ ddlmZ ddlmZm	Z	 ddl
mZmZmZ ddlmZ ddlmZmZmZ g d	¢Zed
k rCddlmZ ndd„ Zde_dd„ ZdS )é    )Úunicode_literalsÚdivisionÚabsolute_importÚprint_functioné   )Úpretty_message)Úbuffer_from_bytesÚbytes_from_bufferé   )Ú	libcryptoÚlibcrypto_version_infoÚhandle_openssl_error)Ú
rand_bytes)Ú	type_nameÚbyte_clsÚ	int_types)Úpbkdf2Ú
pkcs12_kdfr   )r
   )r   c              
   C   s
  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t	jdœ|  ƒ }t|ƒ}t	 |t|ƒ|t|ƒ||||¡}t|ƒ t|ƒS )
aæ  
        PBKDF2 from PKCS#5

        :param hash_algorithm:
            The string name of the hash algorithm to use: "sha1", "sha224", "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

        :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Úsha224Úsha256Úsha384Úsha512z†
                hash_algorithm must be one of "sha1", "sha224", "sha256", "sha384",
                "sha512", not %s
                )Ú
isinstancer   Ú	TypeErrorr   r   r   Ú
ValueErrorÚsetÚreprr   ÚEVP_sha1Ú
EVP_sha224Ú
EVP_sha256Ú
EVP_sha384Ú
EVP_sha512r   ÚPKCS5_PBKDF2_HMACÚlenr   r	   )Úhash_algorithmÚpasswordÚsaltÚ
iterationsÚ
key_lengthÚevp_mdÚoutput_bufferÚresult© r-   úQ/var/www/html/kangema/venv/lib/python3.10/site-packages/oscrypto/_openssl/util.pyr      sd   
ü
ü
ü
üû	ûúø
r   Fc           
      C   sZ  t |tƒsttdt|ƒƒƒ‚t |tƒsttdt|ƒƒƒ‚t |tƒs*ttdt|ƒƒƒ‚|dk r7ttdt|ƒƒƒ‚t |tƒsEttdt|ƒƒƒ‚|dk rRttdt|ƒƒƒ‚| tg d¢ƒvrcttd	t| ƒƒƒ‚|tg d
¢ƒvrtttdt|ƒƒƒ‚| 	d¡ 
d¡d }tjtjtjtjtjtjdœ|  ƒ }t|ƒ}t |t|ƒ|t|ƒ|||||¡	}	t|	ƒ t|ƒS )a0  
    KDF from RFC7292 appendix B.2 - https://tools.ietf.org/html/rfc7292#page-19

    :param hash_algorithm:
        The string name of the hash algorithm to use: "md5", "sha1", "sha224", "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

    :param id_:
        The ID of the usage - 1 for key, 2 for iv, 3 for mac

    :raises:
        ValueError - when any of the parameters contain an invalid value
        TypeError - when any of the parameters are of the wrong type

    :return:
        The derived key as a byte string
    z@
            password must be a byte string, not %s
            z<
            salt must be a byte string, not %s
            z?
            iterations must be an integer, not %s
            r
   zC
            iterations must be greater than 0 - is %s
            z?
            key_length must be an integer, not %s
            zC
            key_length must be greater than 0 - is %s
            )Úmd5r   r   r   r   r   z
            hash_algorithm must be one of "md5", "sha1", "sha224", "sha256",
            "sha384", "sha512", not %s
            )r
   r   é   z<
            id_ must be one of 1, 2, 3, not %s
            zutf-8zutf-16bes     )r   r   r   r   r   r   r   r   r   ÚdecodeÚencoder   ÚEVP_md5r   r   r    r!   r"   r   ÚPKCS12_key_gen_unir$   r   r	   )
r%   r&   r'   r(   r)   Úid_Úutf16_passwordÚdigest_typer+   r,   r-   r-   r.   r   {   s€   
ü
ü
üü
üüûüúù	÷r   N)Ú
__future__r   r   r   r   Ú_errorsr   Ú_ffir   r	   Ú
_libcryptor   r   r   Ú_randr   Ú_typesr   r   r   Ú__all__Ú_pkcs5r   Úpure_pythonr   r-   r-   r-   r.   Ú<module>   s   a