Engine.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. /**
  3. * @package Kohana/Encrypt
  4. * @author Kohana Team
  5. * @copyright (c) 2007-2012 Kohana Team
  6. * @copyright (c) 2016-2018 Koseven Team
  7. * @license https://koseven.ga/LICENSE.md
  8. */
  9. abstract class Kohana_Encrypt_Engine {
  10. /**
  11. * @var string Encryption key
  12. */
  13. protected $_key;
  14. /**
  15. * @var string mcrypt mode
  16. */
  17. protected $_mode;
  18. /**
  19. * @var string mcrypt cipher
  20. */
  21. protected $_cipher;
  22. /**
  23. * Creates a new mcrypt wrapper.
  24. *
  25. * @param mixed $key_config mcrypt key or config array
  26. * @param string $mode mcrypt mode
  27. * @param string $cipher mcrypt cipher
  28. */
  29. public function __construct($key_config, $mode = NULL, $cipher = NULL)
  30. {
  31. if (is_array($key_config))
  32. {
  33. if (isset($key_config['key']))
  34. {
  35. $this->_key = $key_config['key'];
  36. }
  37. else
  38. {
  39. // No default encryption key is provided!
  40. throw new Kohana_Exception('No encryption key is defined in the encryption configuration');
  41. }
  42. if (isset($key_config['mode']))
  43. {
  44. $this->_mode = $key_config['mode'];
  45. }
  46. // Mode not specified in config array, use argument
  47. else if ($mode !== NULL)
  48. {
  49. $this->_mode = $mode;
  50. }
  51. if (isset($key_config['cipher']))
  52. {
  53. $this->_cipher = $key_config['cipher'];
  54. }
  55. // Cipher not specified in config array, use argument
  56. else if ($cipher !== NULL)
  57. {
  58. $this->_cipher = $cipher;
  59. }
  60. }
  61. else if (is_string($key_config))
  62. {
  63. // Store the key, mode, and cipher
  64. $this->_key = $key_config;
  65. $this->_mode = $mode;
  66. $this->_cipher = $cipher;
  67. }
  68. else
  69. {
  70. // No default encryption key is provided!
  71. throw new Kohana_Exception('No encryption key is defined in the encryption configuration');
  72. }
  73. }
  74. abstract public function encrypt($data, $iv);
  75. abstract public function decrypt($data);
  76. }