You are here

protected function TfaBasePlugin::encrypt in Two-factor Authentication (TFA) 7.2

Encrypt a plaintext string.

Should be used when writing codes to storage.

Parameters

string $text: The plaintext to be encrypted.

Return value

string The encrypted text.

1 call to TfaBasePlugin::encrypt()
TfaTestTotp::setInStore in tests/includes/tfa_test.totp.inc
One-off methods that allow for testing base plugin encryption.

File

./tfa.inc, line 620
TFA module classes.

Class

TfaBasePlugin
Base plugin class.

Code

protected function encrypt($text) {

  // Backwards compatibility with Mcrypt.
  if (!extension_loaded('openssl') && extension_loaded('mcrypt')) {
    return $this
      ->encryptWithMcrypt($text);
  }
  $iv = drupal_random_bytes(16);

  // Using 1 instead of the constant OPENSSL_RAW_DATA, for PHP 5.3.
  $ciphertext = openssl_encrypt($text, 'aes-256-cbc', $this->encryptionKey, 1, $iv);
  $crypto_data = array(
    'version' => self::CRYPT_VERSION,
    'iv_base64' => base64_encode($iv),
    'ciphertext_base64' => base64_encode($ciphertext),
  );
  $json_encoded_crypto_data = drupal_json_encode($crypto_data);
  return $json_encoded_crypto_data;
}