PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/kohana_core/system/libraries/Encrypt.php

https://gitlab.com/vince.omega/mcb-nov-build
PHP | 164 lines | 79 code | 25 blank | 60 comment | 10 complexity | 952a4ea3e73d913d64a22a66b3862c3a MD5 | raw file
  1. <?php defined('SYSPATH') OR die('No direct access allowed.');
  2. /**
  3. * The Encrypt library provides two-way encryption of text and binary strings
  4. * using the MCrypt extension.
  5. * @see http://php.net/mcrypt
  6. *
  7. * $Id: Encrypt.php 4072 2009-03-13 17:20:38Z jheathco $
  8. *
  9. * @package Core
  10. * @author Kohana Team
  11. * @copyright (c) 2007-2008 Kohana Team
  12. * @license http://kohanaphp.com/license.html
  13. */
  14. class Encrypt_Core {
  15. // OS-dependant RAND type to use
  16. protected static $rand;
  17. // Configuration
  18. protected $config;
  19. /**
  20. * Returns a singleton instance of Encrypt.
  21. *
  22. * @param array configuration options
  23. * @return Encrypt_Core
  24. */
  25. public static function instance($config = NULL)
  26. {
  27. static $instance;
  28. // Create the singleton
  29. empty($instance) and $instance = new Encrypt((array) $config);
  30. return $instance;
  31. }
  32. /**
  33. * Loads encryption configuration and validates the data.
  34. *
  35. * @param array|string custom configuration or config group name
  36. * @throws Kohana_Exception
  37. */
  38. public function __construct($config = FALSE)
  39. {
  40. if ( ! defined('MCRYPT_ENCRYPT'))
  41. throw new Kohana_Exception('encrypt.requires_mcrypt');
  42. if (is_string($config))
  43. {
  44. $name = $config;
  45. // Test the config group name
  46. if (($config = Kohana::config('encryption.'.$config)) === NULL)
  47. throw new Kohana_Exception('encrypt.undefined_group', $name);
  48. }
  49. if (is_array($config))
  50. {
  51. // Append the default configuration options
  52. $config += Kohana::config('encryption.default');
  53. }
  54. else
  55. {
  56. // Load the default group
  57. $config = Kohana::config('encryption.default');
  58. }
  59. if (empty($config['key']))
  60. throw new Kohana_Exception('encrypt.no_encryption_key');
  61. // Find the max length of the key, based on cipher and mode
  62. $size = mcrypt_get_key_size($config['cipher'], $config['mode']);
  63. if (strlen($config['key']) > $size)
  64. {
  65. // Shorten the key to the maximum size
  66. $config['key'] = substr($config['key'], 0, $size);
  67. }
  68. // Find the initialization vector size
  69. $config['iv_size'] = mcrypt_get_iv_size($config['cipher'], $config['mode']);
  70. // Cache the config in the object
  71. $this->config = $config;
  72. Kohana::log('debug', 'Encrypt Library initialized');
  73. }
  74. /**
  75. * Encrypts a string and returns an encrypted string that can be decoded.
  76. *
  77. * @param string data to be encrypted
  78. * @return string encrypted data
  79. */
  80. public function encode($data)
  81. {
  82. // Set the rand type if it has not already been set
  83. if (Encrypt::$rand === NULL)
  84. {
  85. if (KOHANA_IS_WIN)
  86. {
  87. // Windows only supports the system random number generator
  88. Encrypt::$rand = MCRYPT_RAND;
  89. }
  90. else
  91. {
  92. if (defined('MCRYPT_DEV_URANDOM'))
  93. {
  94. // Use /dev/urandom
  95. Encrypt::$rand = MCRYPT_DEV_URANDOM;
  96. }
  97. elseif (defined('MCRYPT_DEV_RANDOM'))
  98. {
  99. // Use /dev/random
  100. Encrypt::$rand = MCRYPT_DEV_RANDOM;
  101. }
  102. else
  103. {
  104. // Use the system random number generator
  105. Encrypt::$rand = MCRYPT_RAND;
  106. }
  107. }
  108. }
  109. if (Encrypt::$rand === MCRYPT_RAND)
  110. {
  111. // The system random number generator must always be seeded each
  112. // time it is used, or it will not produce true random results
  113. mt_srand();
  114. }
  115. // Create a random initialization vector of the proper size for the current cipher
  116. $iv = mcrypt_create_iv($this->config['iv_size'], Encrypt::$rand);
  117. // Encrypt the data using the configured options and generated iv
  118. $data = mcrypt_encrypt($this->config['cipher'], $this->config['key'], $data, $this->config['mode'], $iv);
  119. // Use base64 encoding to convert to a string
  120. return base64_encode($iv.$data);
  121. }
  122. /**
  123. * Decrypts an encoded string back to its original value.
  124. *
  125. * @param string encoded string to be decrypted
  126. * @return string decrypted data
  127. */
  128. public function decode($data)
  129. {
  130. // Convert the data back to binary
  131. $data = base64_decode($data);
  132. // Extract the initialization vector from the data
  133. $iv = substr($data, 0, $this->config['iv_size']);
  134. // Remove the iv from the data
  135. $data = substr($data, $this->config['iv_size']);
  136. // Return the decrypted data, trimming the \0 padding bytes from the end of the data
  137. return rtrim(mcrypt_decrypt($this->config['cipher'], $this->config['key'], $data, $this->config['mode'], $iv), "\0");
  138. }
  139. } // End Encrypt