Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.
   1  <?php
   2  /**
   3   * Copyright 2012-2017 Horde LLC (http://www.horde.org/)
   4   *
   5   * See the enclosed file LICENSE for license information (LGPL). If you
   6   * did not receive this file, see http://www.horde.org/licenses/lgpl21.
   7   *
   8   * @author   Michael Slusarz <slusarz@horde.org>
   9   * @category Horde
  10   * @license  http://www.horde.org/licenses/lgpl21 LGPL 2.1
  11   * @package  Crypt_Blowfish
  12   */
  13  
  14  /**
  15   * Abstract base driver class for blowfish encryption.
  16   *
  17   * @author    Michael Slusarz <slusarz@horde.org>
  18   * @category  Horde
  19   * @copyright 2012-2017 Horde LLC
  20   * @license   http://www.horde.org/licenses/lgpl21 LGPL 2.1
  21   * @package   Crypt_Blowfish
  22   */
  23  abstract class Horde_Crypt_Blowfish_Base
  24  {
  25      /**
  26       * Cipher method.
  27       *
  28       * @var string
  29       */
  30      public $cipher;
  31  
  32      /**
  33       * Initialization vector.
  34       *
  35       * @var string
  36       */
  37      public $iv = null;
  38  
  39      /**
  40       * Encryption key.
  41       *
  42       * @var string
  43       */
  44      public $key;
  45  
  46      /**
  47       * Is this driver supported on this system?
  48       *
  49       * @return boolean  True if supported.
  50       */
  51      public static function supported()
  52      {
  53          return true;
  54      }
  55  
  56      /**
  57       * Constructor.
  58       *
  59       * @param string $cipher  Either 'ecb' or 'cbc'.
  60       */
  61      public function __construct($cipher)
  62      {
  63          $this->cipher = $cipher;
  64      }
  65  
  66      /**
  67       * Encrypts a string.
  68       *
  69       * @param string $text  The string to encrypt.
  70       *
  71       * @return string  The ciphertext.
  72       * @throws Horde_Crypt_Blowfish_Exception
  73       */
  74      abstract public function encrypt($text);
  75  
  76      /**
  77       * Decrypts a string.
  78       *
  79       * @param string $text  The string to encrypt.
  80       *
  81       * @return string  The ciphertext.
  82       * @throws Horde_Crypt_Blowfish_Exception
  83       */
  84      abstract public function decrypt($text);
  85  
  86      /**
  87       * Sets the initialization vector (required for CBC mode).
  88       *
  89       * @param string $iv  Initialization vector.
  90       */
  91      public function setIv($iv = null)
  92      {
  93          $this->iv = is_null($iv)
  94              ? substr(new Horde_Support_Randomid(), 0, 8)
  95              : $iv;
  96      }
  97  
  98      /**
  99       * Pad text to match blocksize length.
 100       *
 101       * @param string $text     Unpadded text.
 102       * @param boolean $ignore  Don't pad if already at blocksize length.
 103       *
 104       * @return string  Padded text.
 105       */
 106      protected function _pad($text, $ignore = false)
 107      {
 108          $blocksize = Horde_Crypt_Blowfish::BLOCKSIZE;
 109          $padding = $blocksize - (strlen($text) % $blocksize);
 110  
 111          return ($ignore && ($padding == $blocksize))
 112              ? $text
 113              : $text . str_repeat(chr($padding), $padding);
 114      }
 115  
 116      /**
 117       * Unpad text from blocksize boundary.
 118       *
 119       * @param string $text  Padded text.
 120       *
 121       * @return string  Unpadded text.
 122       */
 123      protected function _unpad($text)
 124      {
 125          return substr($text, 0, ord(substr($text, -1)) * -1);
 126      }
 127  
 128  }