Encrypt.php 3.0 KB

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