Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.
   1  <?php
   2  /**
   3   * Copyright 2005-2008 Matthew Fonda <mfonda@php.net>
   4   * Copyright 2008 Philippe Jausions <jausions@php.net>
   5   * Copyright 2012-2017 Horde LLC (http://www.horde.org/)
   6   *
   7   * See the enclosed file LICENSE for license information (LGPL). If you
   8   * did not receive this file, see http://www.horde.org/licenses/lgpl21.
   9   *
  10   * @author   Matthew Fonda <mfonda@php.net>
  11   * @author   Philippe Jausions <jausions@php.net>
  12   * @author   Michael Slusarz <slusarz@horde.org>
  13   * @category Horde
  14   * @license  http://www.horde.org/licenses/lgpl21 LGPL 2.1
  15   * @package  Crypt_Blowfish
  16   */
  17  
  18  /**
  19   * Mcrypt driver for blowfish encryption.
  20   *
  21   * @author    Matthew Fonda <mfonda@php.net>
  22   * @author    Philippe Jausions <jausions@php.net>
  23   * @author    Michael Slusarz <slusarz@horde.org>
  24   * @category  Horde
  25   * @copyright 2005-2008 Matthew Fonda
  26   * @copyright 2008 Philippe Jausions
  27   * @copyright 2012-2017 Horde LLC
  28   * @license   http://www.horde.org/licenses/lgpl21 LGPL 2.1
  29   * @package   Crypt_Blowfish
  30   */
  31  class Horde_Crypt_Blowfish_Mcrypt extends Horde_Crypt_Blowfish_Base
  32  {
  33      /**
  34       * Mcrypt resource.
  35       *
  36       * @var resource
  37       */
  38      private $_mcrypt;
  39  
  40      /**
  41       */
  42      public static function supported()
  43      {
  44          return PHP_VERSION_ID < 70100 && extension_loaded('mcrypt');
  45      }
  46  
  47      /**
  48       */
  49      public function __construct($cipher)
  50      {
  51          parent::__construct($cipher);
  52  
  53          $this->_mcrypt = mcrypt_module_open(MCRYPT_BLOWFISH, '', $cipher, '');
  54      }
  55  
  56      /**
  57       */
  58      public function encrypt($text)
  59      {
  60          mcrypt_generic_init($this->_mcrypt, $this->key, empty($this->iv) ? str_repeat('0', Horde_Crypt_Blowfish::IV_LENGTH) : $this->iv);
  61          $out = mcrypt_generic($this->_mcrypt, $this->_pad($text));
  62          mcrypt_generic_deinit($this->_mcrypt);
  63  
  64          return $out;
  65      }
  66  
  67      /**
  68       */
  69      public function decrypt($text)
  70      {
  71          mcrypt_generic_init($this->_mcrypt, $this->key, empty($this->iv) ? str_repeat('0', Horde_Crypt_Blowfish::IV_LENGTH) : $this->iv);
  72          $out = mdecrypt_generic($this->_mcrypt, $this->_pad($text, true));
  73          mcrypt_generic_deinit($this->_mcrypt);
  74  
  75          return $this->_unpad($out);
  76      }
  77  
  78      /**
  79       */
  80      public function setIv($iv = null)
  81      {
  82          $this->iv = is_null($iv)
  83              ? mcrypt_create_iv(Horde_Crypt_Blowfish::IV_LENGTH, MCRYPT_RAND)
  84              : $iv;
  85      }
  86  
  87  }