Thanks for your clarification. Here comes two use cases about hashing and encryption/decryption.
- Hash and match a password using PHP functions
PHP Code:
/* 1. "Register a new password" use case */
$submitted_password = $this->input->post('password');
$password_hash = password_hash($submitted_password, PASSWORD_DEFAULT);
// Save $password_hash somewhere (database, file, etc)
/* 2. "Verify if previously saved password matches the submitted one" use case */
$submitted_password = $this->input->post('password');
$password_hash = ... // Retrieve previously saved $password_hash
$password_matches = password_verify($submitted_password, $password_hash);
if ($password_matches) {
// Access granted
}
- Encrypt and decrypt a string using CI Encryption library
PHP Code:
$this->load->library('encryption');
$plain_text = 'This is a plain-text message!';
$ciphertext = $this->encryption->encrypt($plain_text);
// Outputs: This is a plain-text message!
echo $this->encryption->decrypt($ciphertext);
The only thing that is not so clear to me is the length of encryption key which has to be
exactly or
at least of 16 bytes (for AES-128 cipher).