#NEWLINE# def generate_key( self , type , bits ) : #NEWLINE# #INDENT# '\n        Generate a key of a given type, with a given number of a bits\n\n        :param type: The key type (TYPE_RSA or TYPE_DSA)\n        :param bits: The number of bits\n\n        :return: None\n        ' #NEWLINE# if ( not isinstance ( type , int ) ) : #NEWLINE# #INDENT# raise TypeError ( 'type must be an integer' ) #NEWLINE# #UNINDENT# if ( not isinstance ( bits , int ) ) : #NEWLINE# #INDENT# raise TypeError ( 'bits must be an integer' ) #NEWLINE# #UNINDENT# exponent = _lib . BN_new ( ) #NEWLINE# exponent = _ffi . gc ( exponent , _lib . BN_free ) #NEWLINE# _lib . BN_set_word ( exponent , _lib . RSA_F4 ) #NEWLINE# if ( type == TYPE_RSA ) : #NEWLINE# #INDENT# if ( bits <= 0 ) : #NEWLINE# #INDENT# raise ValueError ( 'Invalid number of bits' ) #NEWLINE# #UNINDENT# rsa = _lib . RSA_new ( ) #NEWLINE# result = _lib . RSA_generate_key_ex ( rsa , bits , exponent , _ffi . NULL ) #NEWLINE# if ( result == 0 ) : #NEWLINE# #INDENT# _raise_current_error ( ) #NEWLINE# #UNINDENT# result = _lib . EVP_PKEY_assign_RSA ( self . _pkey , rsa ) #NEWLINE# if ( not result ) : #NEWLINE# #INDENT# _raise_current_error ( ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if ( type == TYPE_DSA ) : #NEWLINE# #INDENT# dsa = _lib . DSA_generate_parameters ( bits , _ffi . NULL , 0 , _ffi . NULL , _ffi . NULL , _ffi . NULL , _ffi . NULL ) #NEWLINE# if ( dsa == _ffi . NULL ) : #NEWLINE# #INDENT# _raise_current_error ( ) #NEWLINE# #UNINDENT# if ( not _lib . DSA_generate_key ( dsa ) ) : #NEWLINE# #INDENT# _raise_current_error ( ) #NEWLINE# #UNINDENT# if ( not _lib . EVP_PKEY_assign_DSA ( self . _pkey , dsa ) ) : #NEWLINE# #INDENT# _raise_current_error ( ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# raise Error ( 'No such key type' ) #NEWLINE# #UNINDENT# #UNINDENT# self . _initialized = True