PageRenderTime 77ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Magento/Crypt.php

https://gitlab.com/axeltizon/magentoV1.9-demopoweraccess
PHP | 170 lines | 70 code | 11 blank | 89 comment | 13 complexity | 686f467400553f9e85fffc40e0b382b5 MD5 | raw file
  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category
  22. * @package _var
  23. * @copyright Copyright (c) 2014 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Class encapsulates cryptographic algorithm
  28. */
  29. class Magento_Crypt
  30. {
  31. /**
  32. * @var string
  33. */
  34. protected $_cipher;
  35. /**
  36. * @var string
  37. */
  38. protected $_mode;
  39. /**
  40. * @var string
  41. */
  42. protected $_initVector;
  43. /**
  44. * Encryption algorithm module handle
  45. *
  46. * @var resource
  47. */
  48. protected $_handle;
  49. /**
  50. * Constructor
  51. *
  52. * @param string $key Secret encryption key.
  53. * It's unsafe to store encryption key in memory, so no getter for key exists.
  54. * @param string $cipher Cipher algorithm (one of the MCRYPT_ciphername constants)
  55. * @param string $mode Mode of cipher algorithm (MCRYPT_MODE_modeabbr constants)
  56. * @param string|bool $initVector Initial vector to fill algorithm blocks.
  57. * TRUE generates a random initial vector.
  58. * FALSE fills initial vector with zero bytes to not use it.
  59. * @throws Magento_Exception
  60. */
  61. public function __construct($key, $cipher = MCRYPT_BLOWFISH, $mode = MCRYPT_MODE_ECB, $initVector = false)
  62. {
  63. $this->_cipher = $cipher;
  64. $this->_mode = $mode;
  65. $this->_handle = mcrypt_module_open($cipher, '', $mode, '');
  66. try {
  67. $maxKeySize = mcrypt_enc_get_key_size($this->_handle);
  68. if (strlen($key) > $maxKeySize) {
  69. throw new Magento_Exception('Key must not exceed ' . $maxKeySize . ' bytes.');
  70. }
  71. $initVectorSize = mcrypt_enc_get_iv_size($this->_handle);
  72. if (true === $initVector) {
  73. /* Generate a random vector from human-readable characters */
  74. $abc = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
  75. $initVector = '';
  76. for ($i = 0; $i < $initVectorSize; $i++) {
  77. $initVector .= $abc{rand(0, strlen($abc) - 1)};
  78. }
  79. } else if (false === $initVector) {
  80. /* Set vector to zero bytes to not use it */
  81. $initVector = str_repeat("\0", $initVectorSize);
  82. } else if (!is_string($initVector) || strlen($initVector) != $initVectorSize) {
  83. throw new Magento_Exception('Init vector must be a string of ' . $initVectorSize . ' bytes.');
  84. }
  85. $this->_initVector = $initVector;
  86. } catch (Exception $e) {
  87. mcrypt_module_close($this->_handle);
  88. throw $e;
  89. }
  90. mcrypt_generic_init($this->_handle, $key, $initVector);
  91. }
  92. /**
  93. * Destructor frees allocated resources
  94. */
  95. public function __destruct()
  96. {
  97. mcrypt_generic_deinit($this->_handle);
  98. mcrypt_module_close($this->_handle);
  99. }
  100. /**
  101. * Retrieve a name of currently used cryptographic algorithm
  102. *
  103. * @return string
  104. */
  105. public function getCipher()
  106. {
  107. return $this->_cipher;
  108. }
  109. /**
  110. * Mode in which cryptographic algorithm is running
  111. *
  112. * @return string
  113. */
  114. public function getMode()
  115. {
  116. return $this->_mode;
  117. }
  118. /**
  119. * Retrieve an actual value of initial vector that has been used to initialize a cipher
  120. *
  121. * @return string
  122. */
  123. public function getInitVector()
  124. {
  125. return $this->_initVector;
  126. }
  127. /**
  128. * Encrypt a data
  129. *
  130. * @param string $data String to encrypt
  131. * @return string
  132. */
  133. public function encrypt($data)
  134. {
  135. if (strlen($data) == 0) {
  136. return $data;
  137. }
  138. return mcrypt_generic($this->_handle, $data);
  139. }
  140. /**
  141. * Decrypt a data
  142. *
  143. * @param string $data String to decrypt
  144. * @return string
  145. */
  146. public function decrypt($data)
  147. {
  148. if (strlen($data) == 0) {
  149. return $data;
  150. }
  151. $data = mdecrypt_generic($this->_handle, $data);
  152. /*
  153. * Returned string can in fact be longer than the unencrypted string due to the padding of the data
  154. * @link http://www.php.net/manual/en/function.mdecrypt-generic.php
  155. */
  156. $data = rtrim($data, "\0");
  157. return $data;
  158. }
  159. }