vendor/ircmaxell/random-lib/lib/RandomLib/AbstractMcryptMixer.php line 83

Open in your IDE?
  1. <?php
  2. /*
  3.  * The RandomLib library for securely generating random numbers and strings in PHP
  4.  *
  5.  * @author     Anthony Ferrara <ircmaxell@ircmaxell.com>
  6.  * @copyright  2011 The Authors
  7.  * @license    http://www.opensource.org/licenses/mit-license.html  MIT License
  8.  * @version    Build @@version@@
  9.  */
  10. /**
  11.  * The Mcrypt abstract mixer class
  12.  *
  13.  * PHP version 5.3
  14.  *
  15.  * @category   PHPCryptLib
  16.  * @package    Random
  17.  * @subpackage Mixer
  18.  *
  19.  * @author     Anthony Ferrara <ircmaxell@ircmaxell.com>
  20.  * @copyright  2013 The Authors
  21.  * @license    http://www.opensource.org/licenses/mit-license.html  MIT License
  22.  *
  23.  * @version    Build @@version@@
  24.  */
  25. namespace RandomLib;
  26. /**
  27.  * The mcrypt abstract mixer class
  28.  *
  29.  * @category   PHPCryptLib
  30.  * @package    Random
  31.  * @subpackage Mixer
  32.  *
  33.  * @author     Anthony Ferrara <ircmaxell@ircmaxell.com>
  34.  * @author     Chris Smith <chris@cs278.org>
  35.  */
  36. abstract class AbstractMcryptMixer extends AbstractMixer
  37. {
  38.     /**
  39.      * mcrypt module resource
  40.      *
  41.      * @var resource
  42.      */
  43.     private $mcrypt;
  44.     /**
  45.      * Block size of cipher
  46.      *
  47.      * @var int
  48.      */
  49.     private $blockSize;
  50.     /**
  51.      * Cipher initialization vector
  52.      *
  53.      * @var string
  54.      */
  55.     private $initv;
  56.     /**
  57.      * {@inheritdoc}
  58.      */
  59.     public static function test()
  60.     {
  61.         return extension_loaded('mcrypt');
  62.     }
  63.     /**
  64.      * Construct mcrypt mixer
  65.      */
  66.     public function __construct()
  67.     {
  68.         $this->mcrypt    mcrypt_module_open($this->getCipher(), ''MCRYPT_MODE_ECB'');
  69.         $this->blockSize mcrypt_enc_get_block_size($this->mcrypt);
  70.         $this->initv     str_repeat(chr(0), mcrypt_enc_get_iv_size($this->mcrypt));
  71.     }
  72.     /**
  73.      * Performs cleanup
  74.      */
  75.     public function __destruct()
  76.     {
  77.         if ($this->mcrypt) {
  78.             mcrypt_module_close($this->mcrypt);
  79.         }
  80.     }
  81.     /**
  82.      * Fetch the cipher for mcrypt.
  83.      *
  84.      * @return string
  85.      */
  86.     abstract protected function getCipher();
  87.     /**
  88.      * {@inheritdoc}
  89.      */
  90.     protected function getPartSize()
  91.     {
  92.         return $this->blockSize;
  93.     }
  94.     /**
  95.      * {@inheritdoc}
  96.      */
  97.     protected function mixParts1($part1$part2)
  98.     {
  99.         return $this->encryptBlock($part1$part2);
  100.     }
  101.     /**
  102.      * {@inheritdoc}
  103.      */
  104.     protected function mixParts2($part1$part2)
  105.     {
  106.         return $this->decryptBlock($part2$part1);
  107.     }
  108.     /**
  109.      * Encrypts a block using the suppied key
  110.      *
  111.      * @param string $input Plaintext to encrypt
  112.      * @param string $key   Encryption key
  113.      *
  114.      * @return string Resulting ciphertext
  115.      */
  116.     private function encryptBlock($input$key)
  117.     {
  118.         if (!$input && !$key) {
  119.             return '';
  120.         }
  121.         $this->prepareCipher($key);
  122.         $result mcrypt_generic($this->mcrypt$input);
  123.         mcrypt_generic_deinit($this->mcrypt);
  124.         return $result;
  125.     }
  126.     /**
  127.      * Derypts a block using the suppied key
  128.      *
  129.      * @param string $input Ciphertext to decrypt
  130.      * @param string $key   Encryption key
  131.      *
  132.      * @return string Resulting plaintext
  133.      */
  134.     private function decryptBlock($input$key)
  135.     {
  136.         if (!$input && !$key) {
  137.             return '';
  138.         }
  139.         $this->prepareCipher($key);
  140.         $result mdecrypt_generic($this->mcrypt$input);
  141.         mcrypt_generic_deinit($this->mcrypt);
  142.         return $result;
  143.     }
  144.     /**
  145.      * Sets up the mcrypt module
  146.      *
  147.      * @param string $key
  148.      *
  149.      * @return void
  150.      */
  151.     private function prepareCipher($key)
  152.     {
  153.         if (!== mcrypt_generic_init($this->mcrypt$key$this->initv)) {
  154.             throw new \RuntimeException('Failed to prepare mcrypt module');
  155.         }
  156.     }
  157. }