Engine.php 1.6 KB

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