ó
ū^Yc           @   s   d  Z  d d l m Z d d l m Z d d l m Z d d l m Z d d l	 m
 Z
 d e f d     YZ d	 e f d
     YZ d S(   s2   pyCrypto Crypto-related routines for oauth2client.iĸĸĸĸ(   t   SHA256(   t   RSA(   t
   PKCS1_v1_5(   t   DerSequence(   t   _helperst   PyCryptoVerifierc           B   s/   e  Z d  Z d   Z d   Z e d    Z RS(   s$   Verifies the signature on a message.c         C   s   | |  _  d S(   s   Constructor.

        Args:
            pubkey: OpenSSL.crypto.PKey (or equiv), The public key to verify
            with.
        N(   t   _pubkey(   t   selft   pubkey(    (    sB   /tmp/pip-build-kpPAdC/oauth2client/oauth2client/_pycrypto_crypt.pyt   __init__   s    c         C   s:   t  j | d d } t j |  j  j t j |  |  S(   sĢ  Verifies a message against a signature.

        Args:
            message: string or bytes, The message to verify. If string, will be
                     encoded to bytes as utf-8.
            signature: string or bytes, The signature on the message.

        Returns:
            True if message was signed by the private key associated with the
            public key that this object was constructed with.
        t   encodings   utf-8(   R   t	   _to_bytesR   t   newR   t   verifyR    (   R   t   messaget	   signature(    (    sB   /tmp/pip-build-kpPAdC/oauth2client/oauth2client/_pycrypto_crypt.pyR   $   s    c         C   sŦ   | r t  j |   }  |  j d d  j   } t  j d j | d d !  } t   } | j |  t   } | j | d  t j	 | d  } n t j	 |   } t
 |  S(   s@  Construct a Verified instance from a string.

        Args:
            key_pem: string, public key in PEM format.
            is_x509_cert: bool, True if key_pem is an X509 cert, otherwise it
                          is expected to be an RSA key in PEM format.

        Returns:
            Verifier instance.
        t    t    i   iĸĸĸĸi    i   (   R   R   t   replacet   splitt   _urlsafe_b64decodet   joinR   t   decodeR   t	   importKeyR   (   t   key_pemt   is_x509_certt   pemLinest   certDert   certSeqt   tbsSeqR   (    (    sB   /tmp/pip-build-kpPAdC/oauth2client/oauth2client/_pycrypto_crypt.pyt   from_string4   s    		(   t   __name__t
   __module__t   __doc__R	   R   t   staticmethodR   (    (    (    sB   /tmp/pip-build-kpPAdC/oauth2client/oauth2client/_pycrypto_crypt.pyR      s   			t   PyCryptoSignerc           B   s2   e  Z d  Z d   Z d   Z e d d   Z RS(   s"   Signs messages with a private key.c         C   s   | |  _  d S(   st   Constructor.

        Args:
            pkey, OpenSSL.crypto.PKey (or equiv), The private key to sign with.
        N(   t   _key(   R   t   pkey(    (    sB   /tmp/pip-build-kpPAdC/oauth2client/oauth2client/_pycrypto_crypt.pyR	   Q   s    c         C   s7   t  j | d d } t j |  j  j t j |   S(   są   Signs a message.

        Args:
            message: string, Message to be signed.

        Returns:
            string, The signature of the message for the given key.
        R
   s   utf-8(   R   R   R   R   R$   t   signR    (   R   R   (    (    sB   /tmp/pip-build-kpPAdC/oauth2client/oauth2client/_pycrypto_crypt.pyR&   Y   s    	t
   notasecretc         C   sF   t  j t  j |    } | r0 t j |  } n t d   t |  S(   s_  Construct a Signer instance from a string.

        Args:
            key: string, private key in PEM format.
            password: string, password for private key file. Unused for PEM
                      files.

        Returns:
            Signer instance.

        Raises:
            NotImplementedError if the key isn't in PEM format.
        sp   No key in PEM format was detected. This implementation can only use the PyCrypto library for keys in PEM format.(   R   t   _parse_pem_keyR   R   R   t   NotImplementedErrorR#   (   t   keyt   passwordt   parsed_pem_keyR%   (    (    sB   /tmp/pip-build-kpPAdC/oauth2client/oauth2client/_pycrypto_crypt.pyR   e   s    	(   R   R    R!   R	   R&   R"   R   (    (    (    sB   /tmp/pip-build-kpPAdC/oauth2client/oauth2client/_pycrypto_crypt.pyR#   N   s
   		N(   R!   t   Crypto.HashR    t   Crypto.PublicKeyR   t   Crypto.SignatureR   t   Crypto.Util.asn1R   t   oauth2clientR   t   objectR   R#   (    (    (    sB   /tmp/pip-build-kpPAdC/oauth2client/oauth2client/_pycrypto_crypt.pyt   <module>   s   6