<s> """<STR_LIT>""" <EOL> import base64 <EOL> from OpenSSL import crypto <EOL> from oauth2client . _helpers import _parse_pem_key <EOL> from oauth2client . _helpers import _to_bytes <EOL> class OpenSSLVerifier ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , pubkey ) : <EOL> """<STR_LIT>""" <EOL> self . _pubkey = pubkey <EOL> def verify ( self , message , signature ) : <EOL> """<STR_LIT>""" <EOL> message = _to_bytes ( message , encoding = '<STR_LIT:utf-8>' ) <EOL> signature = _to_bytes ( signature , encoding = '<STR_LIT:utf-8>' ) <EOL> try : <EOL> crypto . verify ( self . _pubkey , signature , message , '<STR_LIT>' ) <EOL> return True <EOL> except crypto . Error : <EOL> return False <EOL> @ staticmethod <EOL> def from_string ( key_pem , is_x509_cert ) : <EOL> """<STR_LIT>""" <EOL> key_pem = _to_bytes ( key_pem ) <EOL> if is_x509_cert : <EOL> pubkey = crypto . load_certificate ( crypto . FILETYPE_PEM , key_pem ) <EOL> else : <EOL> pubkey = crypto . load_privatekey ( crypto . FILETYPE_PEM , key_pem ) <EOL> return OpenSSLVerifier ( pubkey ) <EOL> class OpenSSLSigner ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , pkey ) : <EOL> """<STR_LIT>""" <EOL> self . _key = pkey <EOL> def sign ( self , message ) : <EOL> """<STR_LIT>""" <EOL> message = _to_bytes ( message , encoding = '<STR_LIT:utf-8>' ) <EOL> return crypto . sign ( self . _key , message , '<STR_LIT>' ) <EOL> @ staticmethod <EOL> def from_string ( key , password = b'<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> key = _to_bytes ( key ) <EOL> parsed_pem_key = _parse_pem_key ( key ) <EOL> if parsed_pem_key : <EOL> pkey = crypto . load_privatekey ( crypto . FILETYPE_PEM , parsed_pem_key ) <EOL> else : <EOL> password = _to_bytes ( password , encoding = '<STR_LIT:utf-8>' ) <EOL> pkey = crypto . load_pkcs12 ( key , password ) . get_privatekey ( ) <EOL> return OpenSSLSigner ( pkey ) <EOL> def pkcs12_key_as_pem ( private_key_bytes , private_key_password ) : <EOL> """<STR_LIT>""" <EOL> private_key_password = _to_bytes ( private_key_password ) <EOL> pkcs12 = crypto . load_pkcs12 ( private_key_bytes , private_key_password ) <EOL> return crypto . dump_privatekey ( crypto . FILETYPE_PEM , <EOL> pkcs12 . get_privatekey ( ) ) </s>
