You are here

class Mcrypt in AES encryption 8.2

Mcrypt plugin implementation

How-to use it $test = \Drupal::service('plugin.manager.aes')->getInstanceById('aes_mcrypt')->encrypt('xxx'); \Drupal::service('plugin.manager.aes')->getInstanceById('aes_mcrypt')->decrypt($test) === 'xxx';

@Cryptor( id = "aes_mcrypt", label = "AES mcrypt", description = "Mcrypt AES encryption plugin.", )

@package Drupal\aes\Plugin\AES

Hierarchy

Expanded class hierarchy of Mcrypt

File

src/Plugin/AES/Mcrypt.php, line 27

Namespace

Drupal\aes\Plugin\AES
View source
class Mcrypt extends AESPluginBase {

  /**
   * Constructor.
   */
  public function __construct() {
    parent::__construct([], 'aes_mcrypt', []);
  }

  /**
   * Reverse the string.
   *
   * {@inheritdoc}
   */
  public function encrypt($data, $key = FALSE, $cipher = FALSE) {
    $config = FileStorageFactory::getActive()
      ->read('aes.settings');
    $iv = base64_decode($config['mcrypt_iv']);
    if (!$key) {
      $key = $config['key'];
    }
    if (!$cipher) {
      $cipher = $config['cipher'];
    }
    $td = mcrypt_module_open($cipher, '', MCRYPT_MODE_CBC, '');
    if (empty($iv)) {
      self::make_iv();
      $config = FileStorageFactory::getActive()
        ->read('aes.settings');
      $iv = base64_decode($config['mcrypt_iv']);
      \Drupal::logger('aes')
        ->warning('No initialization vector found while trying to encrypt! Recreated a new one now and will try to carry on as normal.');
    }
    $ks = mcrypt_enc_get_key_size($td);
    $key = substr(sha1($key), 0, $ks);
    mcrypt_generic_init($td, $key, $iv);
    $encrypted = mcrypt_generic($td, $data);
    mcrypt_generic_deinit($td);
    mcrypt_module_close($td);
    return $encrypted;
  }

  /**
   * Recover previously scrambled string.
   *
   * {@inheritdoc}
   */
  public function decrypt($data, $key = FALSE, $cipher = FALSE) {
    $config = FileStorageFactory::getActive()
      ->read('aes.settings');
    $iv = base64_decode($config['mcrypt_iv']);
    if (!$key) {
      $key = $config['key'];
    }
    if (!$cipher) {
      $cipher = $config['cipher'];
    }
    $td = mcrypt_module_open($cipher, '', MCRYPT_MODE_CBC, '');
    $ks = mcrypt_enc_get_key_size($td);
    if (empty($iv)) {
      \Drupal::logger('aes')
        ->error('No initialization vector found while trying to decrypt with mcrypt. Aborting!');
      return FALSE;
    }
    $key = substr(sha1($key), 0, $ks);
    mcrypt_generic_init($td, $key, $iv);
    $decrypted = mdecrypt_generic($td, $data);
    mcrypt_generic_deinit($td);
    mcrypt_module_close($td);
    return trim($decrypted);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
Mcrypt::decrypt public function Recover previously scrambled string. Overrides AESPluginBase::decrypt
Mcrypt::encrypt public function Reverse the string. Overrides AESPluginBase::encrypt
Mcrypt::__construct public function Constructor. Overrides AESPluginBase::__construct
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.