Encrypt.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. class Kohana_Encrypt {
  10. /**
  11. * @var string default instance name
  12. */
  13. public static $default = 'default';
  14. /**
  15. * @var array Encrypt class instances
  16. */
  17. public static $instances = [];
  18. /**
  19. * @var engine Encryption engine
  20. */
  21. public $_engine = NULL;
  22. /**
  23. * Returns a singleton instance of Encrypt. An encryption key must be
  24. * provided in your "encrypt" configuration file.
  25. *
  26. * $encrypt = Encrypt::instance();
  27. *
  28. * @param string $name configuration group name
  29. * @return Encrypt
  30. */
  31. public static function instance($name = NULL, array $config = NULL)
  32. {
  33. if ($name === NULL)
  34. {
  35. // Use the default instance name
  36. $name = Encrypt::$default;
  37. }
  38. if ( ! isset(Encrypt::$instances[$name]))
  39. {
  40. if ($config === NULL)
  41. {
  42. // Load the configuration data
  43. $config = Kohana::$config->load('encrypt')->$name;
  44. }
  45. if ( ! isset($config['key']))
  46. {
  47. // No default encryption key is provided!
  48. throw new Kohana_Exception('No encryption key is defined in the encryption configuration group: :group',
  49. [':group' => $name]);
  50. }
  51. // Create a new instance
  52. Encrypt::$instances[$name] = new Encrypt($config);
  53. }
  54. return Encrypt::$instances[$name];
  55. }
  56. /**
  57. * Creates a new mcrypt wrapper.
  58. *
  59. * @param string $key_config encryption key or config array
  60. * @param string $mode encryption mode
  61. * @param string $cipher encryption cipher
  62. */
  63. public function __construct($key_config, $mode = NULL, $cipher = NULL)
  64. {
  65. if (is_string($key_config))
  66. {
  67. $this->_engine = new Encrypt_Engine_Mcrypt($key_config, $mode, $cipher);
  68. }
  69. else
  70. {
  71. if ( ! isset($key_config['type']))
  72. {
  73. $key_config['type'] = 'mcrypt';
  74. }
  75. // Set the engine class name
  76. $engine_name = 'Encrypt_Engine_'.ucfirst($key_config['type']);
  77. // Create the engine class
  78. $this->_engine = new $engine_name($key_config);
  79. }
  80. }
  81. /**
  82. * Encrypts a string and returns an encrypted string that can be decoded.
  83. *
  84. * $data = $encrypt->encode($data);
  85. *
  86. * The encrypted binary data is encoded using [base64](http://php.net/base64_encode)
  87. * to convert it to a string. This string can be stored in a database,
  88. * displayed, and passed using most other means without corruption.
  89. *
  90. * @param string $data data to be encrypted
  91. * @return string
  92. */
  93. public function encode($data)
  94. {
  95. // Get an initialization vector
  96. $iv = $this->_create_iv();
  97. return $this->_engine->encrypt($data, $iv);
  98. }
  99. /**
  100. * Decrypts an encoded string back to its original value.
  101. *
  102. * $data = $encrypt->decode($data);
  103. *
  104. * @param string $data encoded string to be decrypted
  105. * @return FALSE if decryption fails
  106. * @return string
  107. */
  108. public function decode($data)
  109. {
  110. return $this->_engine->decrypt($data);
  111. }
  112. /**
  113. * Proxy for the mcrypt_create_iv function - to allow mocking and testing against KAT vectors
  114. *
  115. * @return string the initialization vector or FALSE on error
  116. */
  117. protected function _create_iv()
  118. {
  119. return $this->_engine->create_iv();
  120. }
  121. }