You are here

class MCrypt in DRD Agent 4.0.x

Same name and namespace in other branches
  1. 8.3 src/Crypt/Method/MCrypt.php \Drupal\drd_agent\Crypt\Method\MCrypt

Provides MCrypt encryption functionality.

Hierarchy

Expanded class hierarchy of MCrypt

2 string references to 'MCrypt'
Base::getMethods in src/Crypt/Base.php
Get a list of crypt methods, either just their ids or instances of each.
MCrypt::getLabel in src/Crypt/Method/MCrypt.php
Get the crypt method label.

File

src/Crypt/Method/MCrypt.php, line 14

Namespace

Drupal\drd_agent\Crypt\Method
View source
class MCrypt extends BaseMethod {
  private $cipher;
  private $mode;
  private $iv;
  private $password;

  /**
   * {@inheritdoc}
   */
  public function __construct(ContainerInterface $container, array $settings = []) {
    parent::__construct($container);
    $this->cipher = $settings['cipher'] ?? 'rijndael-256';
    $this->mode = $settings['mode'] ?? 'cbc';
    $this->password = $settings['password'] ?? '';
  }

  /**
   * {@inheritdoc}
   */
  public function getLabel() : string {
    return 'MCrypt';
  }

  /**
   * {@inheritdoc}
   */
  public function getCipher() : string {
    return $this->cipher;
  }

  /**
   * {@inheritdoc}
   */
  public function getPassword() : string {
    return base64_decode($this->password);
  }

  /**
   * {@inheritdoc}
   */
  public function isAvailable() : bool {
    return function_exists('mcrypt_encrypt');
  }

  /**
   * {@inheritdoc}
   */
  public function getCipherMethods() : array {
    return [
      'rijndael-128',
      'rijndael-192',
      'rijndael-256',
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function getIv() : string {
    if (empty($this->iv)) {
      $nonceSize = mcrypt_get_iv_size($this->cipher, $this->mode);

      /** @noinspection CryptographicallySecureRandomnessInspection */
      $this->iv = mcrypt_create_iv($nonceSize, MCRYPT_DEV_URANDOM);
    }
    return $this->iv;
  }

  /**
   * {@inheritdoc}
   */
  public function encrypt(array $args) : string {
    return mcrypt_encrypt($this->cipher, $this
      ->getPassword(), serialize($args), $this->mode, $this
      ->getIv());
  }

  /**
   * {@inheritdoc}
   */
  public function decrypt($body, $iv) {
    $this->iv = $iv;

    /** @noinspection UnserializeExploitsInspection */
    return unserialize(mcrypt_decrypt($this->cipher, $this
      ->getPassword(), $body, $this->mode, $this->iv));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BaseMethod::$container protected property
BaseMethod::$logger protected property
BaseMethod::cryptFileExecute private function Callback to encrypt and decrypt files.
BaseMethod::encryptFile public function Encrypt a file. Overrides BaseMethodInterface::encryptFile
MCrypt::$cipher private property
MCrypt::$iv private property
MCrypt::$mode private property
MCrypt::$password private property
MCrypt::decrypt public function Decode, decrypt and unserialize arguments from the other end. Overrides BaseMethodInterface::decrypt
MCrypt::encrypt public function Encrypt and encode any list of arguments. Overrides BaseMethodInterface::encrypt
MCrypt::getCipher public function Get the selected cipher. Overrides BaseMethodInterface::getCipher
MCrypt::getCipherMethods public function Get a list of available cipher methods. Overrides BaseMethodInterface::getCipherMethods
MCrypt::getIv public function Get an initialiation vector. Overrides BaseMethodInterface::getIv
MCrypt::getLabel public function Get the crypt method label. Overrides BaseMethodInterface::getLabel
MCrypt::getPassword public function Get the password. Overrides BaseMethodInterface::getPassword
MCrypt::isAvailable public function Find out if the crypt method is available. Overrides BaseMethodInterface::isAvailable
MCrypt::__construct public function BaseMethod constructor. Overrides BaseMethod::__construct