<?php

class Seguranca_model extends CI_Model {
	
	private $table = "sys_keys_rsa";
		
	function set_public_key($key) {
	    
	    $data['public_key'] = $key;
	    
		if ($this->db->insert($this->table, $data)){
		    return $this->db->insert_id();
		}
		else {
		    var_dump('error');
		    log_message('error','Erro ao gravar no banco a chave publica.');
			return NULL;
		}
	}
	
	function set_private_key($id_rsa,$key) {
	
		$this->db->where('id_rsa',$id_rsa);
		$data['private_key'] = $key;
	
		if ($this->db->update ( $this->table, $data ))
			return true;
	   else {
			log_message('error','Erro ao gravar no banco a chave privada.');
			return NULL;
		}
	}
	
	function get_public_key($id_rsa)
	{
	    $this->db->select('public_key');
	    $this->db->where('id_rsa',$id_rsa);
	    $query = $this->db->get($this->table);
		if ($query->num_rows() == 1 ) return $query->row()->public_key;
		return NULL;
	}
	
	function get_private_key($id_rsa)
	{
		$this->db->select('private_key');
		$this->db->where('id_rsa',$id_rsa);
		$query = $this->db->get($this->table);
		if ($query->num_rows() == 1 ) return $query->row()->private_key;
		return NULL;
	}
	
	function delete_key($id_rsa)
	{
		$this->db->where('id_rsa',$id_rsa);
		if ($this->db->delete($this->table))
		{
		    return true;
		}
		else 
		{	
		    return NULL;
		}
	}
}

?>