o
    Œõ±j  ã                   @   sŠ   d dl mZmZmZmZ d dlZd dlZd dlZddlm	Z	m
Z
 ddlmZ ddlmZmZmZ ejdk r8eZndd	„ Zd
gZdd
„ ZdS )é    )Úunicode_literalsÚdivisionÚabsolute_importÚprint_functionNé   )Úint_from_bytesÚint_to_bytes)Úpretty_message)Ú	type_nameÚbyte_clsÚ	int_types)é   c                 C   s
   t | gƒS )N)Úbytes)Únum© r   úƒ/root/aizidognhua/tmp/workspace/projects/ec89d86c-575f-41c9-af57-ac45cbdbf775/venv/lib/python3.10/site-packages/oscrypto/_pkcs12.pyÚchr_cls   s   
r   Ú
pkcs12_kdfc                 C   sp  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t| ƒ}dddddddœ|  }| dv r•d}	nd}	t|ƒ|	 }
d}|dkrÈ|	tt tt|ƒƒ|	 ¡ƒ }t|ƒ|k rÂ||7 }t|ƒ|k s¸|d|… }d}|dkró|	tt tt|ƒƒ|	 ¡ƒ }t|ƒ|k rí||7 }t|ƒ|k sã|d|… }|| }tt t|ƒ| ¡ƒ}d||  }td|d ƒD ]¢}||
| ƒ ¡ }td|d ƒD ]	}||ƒ ¡ }�q ||k �r‘d}t|ƒ|	k �rC||7 }t|ƒ|	k �s8t|d|	… ƒd }tdt|ƒ|	 ƒD ]:}||	 }|d |	 }|||… }tt|ƒ| ƒ}t|ƒ}||	k�r�|||	 d… }|d|… | ||d…  }�qV|d | }t||ƒ}|d|… |d|…  ||| d…  }�q|d|… S )aœ  
    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

    :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
            )Úmd5Úsha1Úsha224Úsha256Úsha384Úsha512z�
            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     é   é   é   é    é0   é@   )r   r   é€   ó    r   ó    r   N)Ú
isinstancer   Ú	TypeErrorr	   r
   r   Ú
ValueErrorÚreprÚsetÚdecodeÚencodeÚgetattrÚhashlibr   ÚintÚmathÚceilÚfloatÚlenÚrangeÚdigestr   r   Úmin)Úhash_algorithmÚpasswordÚsaltÚ
iterationsÚ
key_lengthÚid_Úutf16_passwordÚalgoÚuÚvÚdÚsÚs_lenÚpÚp_lenÚiÚcÚar   Úa2Ú_ÚbÚnum2ÚstartÚendÚi_num2Úi_num2_lÚbeginÚto_copyr   r   r   r      s¾   
ü
ü
üü
üüûü
úù	ÿÿ
ÿ
 
,)Ú
__future__r   r   r   r   Úsysr,   r.   Ú_asn1r   r   Ú_errorsr	   Ú_typesr
   r   r   Úversion_infoÚchrr   Ú__all__r   r   r   r   r   Ú<module>   s   
ÿ