/upload/modules/cache/src/filters/MCrypt.php

https://bitbucket.org/niidees/madserve · PHP · 82 lines · 36 code · 10 blank · 36 comment · 2 complexity · 96a9643ca44a66b77a27025c10a68f2b MD5 · raw file

  1. <?php
  2. /**
  3. * FluxBB
  4. *
  5. * LICENSE
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 3 of the License, or any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
  20. *
  21. * @category FluxBB
  22. * @package Cache
  23. * @subpackage Filters
  24. * @copyright Copyright (c) 2011 FluxBB (http://fluxbb.org)
  25. * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
  26. */
  27. /**
  28. * The MCrypt filter encrypts data using the given cipher and secret key.
  29. * http://uk2.php.net/manual/en/book.mcrypt.php
  30. */
  31. namespace fluxbb\cache\filters;
  32. class MCrypt implements \fluxbb\cache\Filter
  33. {
  34. const DEFAULT_CIPHER = MCRYPT_RIJNDAEL_128;
  35. const DEFAULT_MODE = MCRYPT_MODE_ECB;
  36. private $key;
  37. private $cipher;
  38. private $mode;
  39. private $iv;
  40. /**
  41. * Initialise a new MCrypt filter.
  42. *
  43. * @param secret The secret key to encrypt/decrypt data with
  44. * @param cipher The cipher to use, defaults to MCRYPT_RIJNDAEL_128
  45. * @param mode The block cipher mode to use, defaults to MCRYPT_MODE_ECB
  46. */
  47. public function __construct($config)
  48. {
  49. if (!extension_loaded('mcrypt'))
  50. {
  51. throw new \fluxbb\cache\Exception('The MCrypt filter requires the MCrypt extension.');
  52. }
  53. if (!isset($config['secret']))
  54. {
  55. throw new \fluxbb\cache\Exception('A secret is required to encrypt data.');
  56. }
  57. $this->key = md5($config['secret']);
  58. $this->cipher = isset($config['cipher']) ? $config['cipher'] : self::DEFAULT_CIPHER;
  59. $this->mode = isset($config['mode']) ? $config['mode'] : self::DEFAULT_MODE;
  60. $this->iv = mcrypt_create_iv(mcrypt_get_iv_size($this->cipher, $this->mode), MCRYPT_RAND);
  61. }
  62. public function encode($data)
  63. {
  64. $data = mcrypt_encrypt($this->cipher, $this->key, $data, $this->mode, $this->iv);
  65. return base64_encode($data);
  66. }
  67. public function decode($data)
  68. {
  69. $data = base64_decode($data);
  70. return mcrypt_decrypt($this->cipher, $this->key, $data, $this->mode, $this->iv);
  71. }
  72. }