ó
Ý˛k^c           @   sL  d  Z  d d l Z d d l Z d d l Z d d l Z d d l Z y d d l m Z e j Z	 Wn' e
 k
 r e j e  j d  n Xd d l m Z d d l m Z e j e  Z d d	 d
 d d d g Z d Z e j   j Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z  d   Z! d   Z" d S(   sü   wrapper around PyCrypto cryptography library

Information sources:
    - http://eli.thegreenplace.net/2010/06/25/aes-encryption-of-files-in-python-with-pycrypto/
    - http://code.activestate.com/recipes/576980-authenticated-encryption-with-pycrypto/

i˙˙˙˙N(   t   AESsV   auxlib.crypt is a pycrypto wrapper, which is not installed in the current environment.i   (   t	   text_type(   t   AuthenticationErrort	   as_base64t   from_base64t   encryptt   decryptt   aes_encryptt   aes_decrypti    c         C   s=   t    } t | |  } t |   } t | |  } | | f S(   N(   t   generate_encryption_keyR   t   generate_hash_from_secret(   t
   secret_keyt   datat   message_encryption_keyt   encrypted_datat   hashed_secrett   encryption_key_encrypted(    (    s9   lib/python2.7/site-packages/conda/_vendor/auxlib/crypt.pyR   "   s
    	c         C   s.   t  |   } t | |  } t | |  } | S(   N(   R
   R   (   R   R   R   R   R   R   (    (    s9   lib/python2.7/site-packages/conda/_vendor/auxlib/crypt.pyR   *   s    c         C   s.   t  |  t  r! |  j d  }  n  t j |   S(   Ns   UTF-8(   t
   isinstanceR   t   encodet   base64t   urlsafe_b64encode(   t   content(    (    s9   lib/python2.7/site-packages/conda/_vendor/auxlib/crypt.pyR   1   s    c         C   s.   t  |  t  r! |  j d  }  n  t j |   S(   Ns   UTF-8(   R   R   R   R   t   urlsafe_b64decode(   R   (    (    s9   lib/python2.7/site-packages/conda/_vendor/auxlib/crypt.pyR   7   s    c           C   s   t  t j t t   S(   sĹ   Create a new, random encryption key for use by this module.

    The encryption key is composed of an AES key and an HMAC signing key.

    Returns:
        str: base64-encoded encryption key

    (   R   t   ost   urandomt   AES_KEY_SIZEt   HMAC_SIG_SIZE(    (    (    s9   lib/python2.7/site-packages/conda/_vendor/auxlib/crypt.pyR	   =   s    	c         C   s(   t  t j t |   j d   j    S(   Ns   UTF-8(   R   t   hashlibt   sha512R   R   t   digest(   t   secret(    (    s9   lib/python2.7/site-packages/conda/_vendor/auxlib/crypt.pyR
   I   s    c         C   sŤ   t  | t  r! | j d  } n  t |   \ } } t |  } t j t  } t j	 | d t j
 d | } | | j |  } t j	 | | t j  j   } t | |  S(   sŚ  Encrypt data with AES-CBC and sign it with HMAC-SHA256

    Arguments:
        base64_encryption_key (str): a base64-encoded string containing an AES encryption key
            and HMAC signing key as generated by generate_encryption_key()
        data (str): a byte string containing the data to be encrypted

    Returns:
        str: the encrypted data as a byte string with the HMAC signature appended to the end

    s   UTF-8t   modet   IV(   R   R   R   t   _extract_keyst   _padR   R   t   AES_BLOCK_SIZER    t   newt   MODE_CBCR   t   hmacR   t   sha256R   R   (   t   base64_encryption_keyR   t   aes_key_bytest   hmac_key_bytest   iv_bytest   ciphert   hmac_signature(    (    s9   lib/python2.7/site-packages/conda/_vendor/auxlib/crypt.pyR   M   s    c         C   sŽ   t  |  } t |   \ } } | t  | t } } t j | | t j  j   | k rh t d   n  | t	  | t	 } } t
 j | t
 j |  } | j |  } t |  S(   s$  Verify HMAC-SHA256 signature and decrypt data with AES-CBC

    Arguments:
        encryption_key (str): a base64-encoded string containing an AES encryption key and HMAC
            signing key as generated by generate_encryption_key()
        data (str): a byte string containing the data decrypted with an HMAC signing key
            appended to the end

    Returns:
        str: a byte string containing the data that was originally encrypted

    Raises:
        AuthenticationError: when the HMAC-SHA256 signature authentication fails

    s   HMAC authentication failed(   R   R!   R   R&   R$   R   R'   R   R   R#   R    R%   R   t   _unpad(   R(   t   base64_dataR   R)   R*   R-   R+   R,   (    (    s9   lib/python2.7/site-packages/conda/_vendor/auxlib/crypt.pyR   d   s    $c         C   s/   t  t |   t  } |  t |  | j d  S(   Ns   UTF-8(   R#   t   lent   chrR   (   t   st   padding_bytes(    (    s9   lib/python2.7/site-packages/conda/_vendor/auxlib/crypt.pyR"      s    c         C   s   |  t  |  j d  d   S(   Ns   UTF-8i˙˙˙˙(   t   ordt   decode(   R2   (    (    s9   lib/python2.7/site-packages/conda/_vendor/auxlib/crypt.pyR.      s    c         C   s    t  |   } | t  | t f S(   N(   R   R   (   t   key_strt	   key_bytes(    (    s9   lib/python2.7/site-packages/conda/_vendor/auxlib/crypt.pyR!      s    (#   t   __doc__R   R   R&   t   loggingR   t   Crypto.CipherR    t
   block_sizeR#   t   ImportErrort	   getLoggert   __name__t   errort   compatR   t
   exceptionsR   t   logt   __all__R   R'   t   digest_sizeR   R   R   R   R   R	   R
   R   R   R"   R.   R!   (    (    (    s9   lib/python2.7/site-packages/conda/_vendor/auxlib/crypt.pyt   <module>   s6   										