U
    ;g                     @   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 rpe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   4/tmp/pip-unpacked-wheel-x1gypflw/oscrypto/_pkcs12.pychr_cls   s    r   
pkcs12_kdfc                 C   sl  t |tsttdt|t |ts8ttdt|t |tsTttdt||dk rnttdt|t |tsttdt||dk rttdt|| tdd	d
dddgkrttdt| |tdddgkrttdt||	d
dd }tt| }ddddddd|  }| dkr6d}	nd}	t||	 }
d}|dkr|	tttt||	  }t||k r||7 }qr|d| }d}|dkr|	tttt||	  }t||k r||7 }q|d| }|| }ttt|| }d ||  }td|d D ]:}||
|  }td|d D ]}|| }qF||k rd}t||	k r||7 }qht|d|	 d }tdt||	 D ]t}||	 }|d |	 }||| }tt|| }t|}||	kr|||	 d! }|d| | ||d!  }q|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   z<
            id_ must be one of 1, 2, 3, not %s
            zutf-8zutf-16bes                  0   @   )r   r   r   r   r   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saltZ
iterationsZ
key_lengthZid_Zutf16_passwordalgouvdsZs_lenpZp_lenicar   Za2_bZnum2startendZi_num2Zi_num2_lbeginZto_copyr   r   r   r      s    




	




 
,)
__future__r   r   r   r   sysr,   r.   Z_asn1r   r   _errorsr	   _typesr
   r   r   version_infochrr   __all__r   r   r   r   r   <module>   s   
