o
                         @   sd   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
 G dd deZG d	d
 d
eZdS )z2pyCrypto Crypto-related routines for oauth2client.    )SHA256)RSA)
PKCS1_v1_5)DerSequence)_helpersc                   @   s,   e Zd ZdZdd Zdd Zedd ZdS )	PyCryptoVerifierz$Verifies the signature on a message.c                 C   
   || _ dS )zConstructor.

        Args:
            pubkey: OpenSSL.crypto.PKey (or equiv), The public key to verify
            with.
        N)_pubkey)selfpubkey r   E/tmp/google-cloud-sdk/lib/third_party/oauth2client/_pycrypto_crypt.py__init__   s   
zPyCryptoVerifier.__init__c                 C   s(   t j|dd}t| jt||S )a  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.
        utf-8encoding)r   	_to_bytesr   newr	   verifyr   )r
   message	signaturer   r   r   r   $   s   
zPyCryptoVerifier.verifyc                 C   s   |r8t | } | dd }t d|dd }t }|| t }||d  t	|d }t
|S t	| }t
|S )a@  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.
                   r      )r   r   replacesplit_urlsafe_b64decodejoinr   decoder   	importKeyr   )key_pemis_x509_certpemLinescertDercertSeqtbsSeqr   r   r   r   from_string4   s   


zPyCryptoVerifier.from_stringN)__name__
__module____qualname____doc__r   r   staticmethodr(   r   r   r   r   r      s    	r   c                   @   s.   e Zd ZdZdd Zdd Zed
ddZd	S )PyCryptoSignerz"Signs messages with a private key.c                 C   r   )ztConstructor.

        Args:
            pkey, OpenSSL.crypto.PKey (or equiv), The private key to sign with.
        N)_key)r
   pkeyr   r   r   r   Q   s   
zPyCryptoSigner.__init__c                 C   s&   t j|dd}t| jt|S )zSigns a message.

        Args:
            message: string, Message to be signed.

        Returns:
            string, The signature of the message for the given key.
        r   r   )r   r   r   r   r/   signr   )r
   r   r   r   r   r1   Y   s   	zPyCryptoSigner.sign
notasecretc                 C   s.   t t | }|rt|}t|S td)a_  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.
        zpNo key in PEM format was detected. This implementation can only use the PyCrypto library for keys in PEM format.)r   _parse_pem_keyr   r   r!   NotImplementedErrorr.   )keypasswordparsed_pem_keyr0   r   r   r   r(   e   s   
zPyCryptoSigner.from_stringN)r2   )r)   r*   r+   r,   r   r1   r-   r(   r   r   r   r   r.   N   s    r.   N)r,   Crypto.Hashr   Crypto.PublicKeyr   Crypto.Signaturer   Crypto.Util.asn1r   oauth2clientr   objectr   r.   r   r   r   r   <module>   s   6