You are here

function password_field_encrypt_decrypt in Password Field 8

Helper function to encrypt-decryt data.

1 call to password_field_encrypt_decrypt()
PasswordField::__unset in src/Plugin/Field/FieldType/PasswordField.php
Unsets the variable.

File

./password_field.module, line 29
Contains password_field.module.

Code

function password_field_encrypt_decrypt($action, $string) {
  $output = FALSE;
  $encrypt_method = "AES-256-CBC";
  $secret_key = 'This is my secret key';
  $secret_iv = 'This is my secret iv';

  // Hash.
  $key = hash('sha256', $secret_key);

  // Iv - encrypt method AES-256-CBC expects 16 bytes else you will get a warning.
  $iv = substr(hash('sha256', $secret_iv), 0, 16);
  if ($action == 'encrypt') {
    $output = openssl_encrypt($string, $encrypt_method, $key, 0, $iv);
    $output = base64_encode($output);
  }
  elseif ($action == 'decrypt') {
    $output = openssl_decrypt(base64_decode($string), $encrypt_method, $key, 0, $iv);
  }
  return $output;
}