0
votes

I get that the encryption class produces different output each time the same word/string is encrypted, for example, $this->encrypt->encode("word") ran five times would produce five different encrypted strings.

How can I reference the encrypted string in a DB query if each time I call $this->encrypt->encode("word") gives me something different?

Asked in a different way, is there something I can encrypt with that doesn't have a random value so that each time I encrypt I get the same output for the same input?

3

3 Answers

2
votes

Base64 encoding is not encryption (referring to your own answer). I have not used codeigniter, but I notice on its doc pages that it allows:

$this->encrypt->set_mode();

You could encrypt with ECB mode (MCRYPT_MODE_ECB) for deterministic encryption where the same data always encrypts to the same ciphertext. This way, you encrypt your search string, and it will match the encrypted data in the database.

This is considered a weakness of ECB mode, but in this case, the deterministic behavior may be what you want.

1
votes

I think base64_encode($str) is what I'm looking for.

0
votes

this code works only on php 5.5 or higher

echo password_hash(variable, PASSWORD_DEFAULT);

The first parameter is the password string that needs to be hashed and the second parameter specifies the algorithm that should be used for generating the hash.

The default algorithm is currently bcrypt, but a stronger algorithm may be added as the default later at some point in the future and may generate a larger string. If you are using PASSWORD_DEFAULT in your projects, be sure to store the hash in a column that’s capacity is beyond 60 characters. Setting the column size to 255 might be a good choice. You could also use PASSWORD_BCRYPT as the second parameter. In this case the result will always be 60 characters long.

and to check the password hash here is the syntax

<?php
if (password_verify($oldpassword, $hash)) {
  // Success!
 // the first parameter is your password that's not yet encrypted, the secode is your password  encrypted
 }
 else {
  // Invalid password
 }