Welcome Guest, Not a member yet? Register   Sign In
Encryption uniqueness
#1

[eluser]chiste[/eluser]
Hi

I'll like to store my users emails encrypted (with the ability to uncrypt it later) and check for uniqueness if the user tries to register twice with the same email. The problem is that every time I use the encrypt class I got a different string so it is registered twice in my DB. Maybe I'm doing something wrong.

My Controller
Code:
public function signup() {
$this->load->library('form_validation');
$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|callback_check_email');
if($this->form_validation->run() === FALSE) {
  $this->signup();
} else {
  $this->load->model('user_model');
  if($this->user_model->signup()){
   $this->success();
  } else {
   $this->signup();
  }
}
}

public function check_email($str) {
$this->db->where('email', $this->encrypt->encode($str));
$query = $this->db->get('users');
if ($query->num_rows == 0) {
  return TRUE;
} else {
  $this->form_validation->set_message('check_email', 'All ready registered.');
  return FALSE;
}
}

My Model
Code:
function signup() {
  
$email = $this->input->post('email');
$name = $this->input->post('name');

$data = array(
    'email' => $this->encrypt->encode($email),
    'name' => $name,
);
  
if ($this->db->insert('users', $data)) {
  return TRUE;
} else {
  return FALSE;
}
  
}
#2

[eluser]xerobytez[/eluser]
The encryption class uses random values in the equation so the output will always be different even though the input is the same. If you need to do a comparison then you would need to hash the email with sha1, md5 or one of the many other methods available. Hashes are however one-way, they usually can't be deciphered. So you could store the encrypted email in one field, and the hashed email in another. Then use the hash field for comparison and the encrypted field later on when you need to decrypt it.
#3

[eluser]chiste[/eluser]
Great advise. Thanks!




Theme © iAndrew 2016 - Forum software by © MyBB