<s> from pyelliptic . openssl import OpenSSL <EOL> class Cipher : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , key , iv , do , ciphername = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> self . cipher = OpenSSL . get_cipher ( ciphername ) <EOL> self . ctx = OpenSSL . EVP_CIPHER_CTX_new ( ) <EOL> if do == <NUM_LIT:1> or do == <NUM_LIT:0> : <EOL> k = OpenSSL . malloc ( key , len ( key ) ) <EOL> IV = OpenSSL . malloc ( iv , len ( iv ) ) <EOL> OpenSSL . EVP_CipherInit_ex ( <EOL> self . ctx , self . cipher . get_pointer ( ) , <NUM_LIT:0> , k , IV , do ) <EOL> else : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> @ staticmethod <EOL> def get_all_cipher ( ) : <EOL> """<STR_LIT>""" <EOL> return OpenSSL . cipher_algo . keys ( ) <EOL> @ staticmethod <EOL> def get_blocksize ( ciphername ) : <EOL> cipher = OpenSSL . get_cipher ( ciphername ) <EOL> return cipher . get_blocksize ( ) <EOL> @ staticmethod <EOL> def gen_IV ( ciphername ) : <EOL> cipher = OpenSSL . get_cipher ( ciphername ) <EOL> return OpenSSL . rand ( cipher . get_blocksize ( ) ) <EOL> def update ( self , input ) : <EOL> i = OpenSSL . c_int ( <NUM_LIT:0> ) <EOL> buffer = OpenSSL . malloc ( b"<STR_LIT>" , len ( input ) + self . cipher . get_blocksize ( ) ) <EOL> inp = OpenSSL . malloc ( input , len ( input ) ) <EOL> if OpenSSL . EVP_CipherUpdate ( self . ctx , OpenSSL . byref ( buffer ) , <EOL> OpenSSL . byref ( i ) , inp , len ( input ) ) == <NUM_LIT:0> : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> return buffer . raw [ <NUM_LIT:0> : i . value ] <EOL> def final ( self ) : <EOL> i = OpenSSL . c_int ( <NUM_LIT:0> ) <EOL> buffer = OpenSSL . malloc ( b"<STR_LIT>" , self . cipher . get_blocksize ( ) ) <EOL> if ( OpenSSL . EVP_CipherFinal_ex ( self . ctx , OpenSSL . byref ( buffer ) , <EOL> OpenSSL . byref ( i ) ) ) == <NUM_LIT:0> : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> return buffer . raw [ <NUM_LIT:0> : i . value ] <EOL> def ciphering ( self , input ) : <EOL> """<STR_LIT>""" <EOL> buff = self . update ( input ) <EOL> return buff + self . final ( ) <EOL> def __del__ ( self ) : <EOL> OpenSSL . EVP_CIPHER_CTX_cleanup ( self . ctx ) <EOL> OpenSSL . EVP_CIPHER_CTX_free ( self . ctx ) </s>
