Просмотр файла engine/classes/Crypt.php

Размер файла: 6.56Kb
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3.  
  4.  
  5. /**
  6. * @package Crypt class
  7. * @category Cryptography
  8. * @author Arthur Furlan <arthur.furlan@gmail.com>
  9. * @copyright 2006 (c) - Arthur Furlan
  10. * @license GPL v3.0 {@link http://gnu.org/licenses/gpl.txt}
  11. */
  12.  
  13. class Crypt {
  14.  
  15. // {{{ Constants
  16. /* Returning modes */
  17. const MODE_BIN = 0; // same length of the original data
  18. const MODE_B64 = 1; // about 66% bigger
  19. const MODE_HEX = 2; // exactly 100% bigger
  20.  
  21. // }}}
  22. // {{{ Attributes
  23.  
  24. /**
  25. * The private key used in the cryptography method.
  26. * NOTE: This value should be as strange as possible.
  27. *
  28. * @name $_key
  29. * @access private
  30. * @var string
  31. */
  32. private $_key = __CLASS__;
  33.  
  34. /**
  35. * The returning mode of the cryptography method.
  36. *
  37. * @name $_mode
  38. * @access private
  39. * @var integer
  40. * @see Crypt::{MODE_BIN, MODE_B64, MODE_HEX}
  41. */
  42. private $_mode = Crypt::MODE_HEX;
  43.  
  44. // }}}
  45. // {{{ function __construct()
  46.  
  47. /**
  48. * Configure the new object, setting the mode and key.
  49. *
  50. * @name __construct()
  51. * @access public
  52. * @param [$mode] integer
  53. * @param [$key] integer
  54. * @return void
  55. */
  56. function __construct($mode = null, $key = null) {
  57. is_null($mode) || ($this->Mode = $mode);
  58. is_null($key) || ($this->Key = $key);
  59. }
  60.  
  61. // }}}
  62. // {{{ function __toString()
  63.  
  64. /**
  65. * Overload of the object conversion to string.
  66. *
  67. * @name __toString()
  68. * @access public
  69. * @method void
  70. * @return string
  71. */
  72. function __toString() {
  73. return __CLASS__ . " object\n"
  74. . "(\n"
  75. . " [Key] => {$this->_key}\n"
  76. . " [Mode] => {$this->_mode}\n"
  77. . ")\n";
  78. }
  79.  
  80. // }}}
  81. // {{{ function __set()
  82.  
  83. /**
  84. * Properties write methods.
  85. *
  86. * @name __set()
  87. * @param $property string
  88. * @param $value mixed
  89. * @return void
  90. */
  91. function __set($property, $value) {
  92. switch ($property) {
  93. case 'Key' : return $this->_setKey($value);
  94. case 'Mode': return $this->_setMode($value);
  95. }
  96. }
  97.  
  98. // }}}
  99. // {{{ function __get()
  100.  
  101. /**
  102. * Properties read methods.
  103. *
  104. * @name __get()
  105. * @param $key string
  106. * @return void
  107. */
  108. function __get($property) {
  109. switch ($property) {
  110. case 'Key' : return $this->_key;
  111. case 'Mode': return $this->_mode;
  112. }
  113. }
  114.  
  115. // }}}
  116. // {{{ public function encrypt()
  117.  
  118. /**
  119. * Encrypt the data using the current returning mode.
  120. *
  121. * @name encrypt()
  122. * @access public
  123. * @param $data mixed
  124. * @return string
  125. */
  126. public function encrypt($data) {
  127. $data = (string) $data;
  128. for ($i=0;$i<strlen($data);$i++)
  129. @$encrypt .= $data[$i] ^
  130. $this->_key[$i % strlen($this->_key)];
  131. if ($this->_mode === Crypt::MODE_B64)
  132. return base64_encode(@$encrypt);
  133. if ($this->_mode === Crypt::MODE_HEX)
  134. return $this->_encodeHexadecimal(@$encrypt);
  135. return @$encrypt;
  136. }
  137.  
  138. // }}}
  139. // {{{ public function decrypt()
  140.  
  141. /**
  142. * Decrypt the data using the current returning mode.
  143. * NOTE: You must use the same $_mode of the creation process.
  144. *
  145. * @name decrypt()
  146. * @access public
  147. * @param $crypt string
  148. * @return string
  149. */
  150. public function decrypt($crypt) {
  151. if ($this->_mode === Crypt::MODE_HEX)
  152. $crypt = $this->_decodeHexadecimal($crypt);
  153. if ($this->_mode === Crypt::MODE_B64)
  154. $crypt = (string)base64_decode($crypt);
  155. for ($i=0;$i<strlen($crypt);$i++)
  156. @$data .= $crypt[$i] ^ $this->_key[$i % strlen($this->_key)];
  157. return @$data;
  158. }
  159.  
  160. // }}}
  161. // {{{ public static function supportedModes()
  162.  
  163. /**
  164. * Return the list containing all supported modes.
  165. *
  166. * @name supportedModes()
  167. * @access public
  168. * @param void
  169. * @return void
  170. */
  171. public static function supportedModes() {
  172. return array(
  173. Crypt::MODE_BIN,
  174. Crypt::MODE_B64,
  175. Crypt::MODE_HEX
  176. );
  177. }
  178.  
  179. // }}}
  180. // {{{ protected static function _isSupportedMode()
  181.  
  182. /**
  183. * Checks if $mode is a valid returning mode of the class.
  184. *
  185. * @name _isSupportedMode()
  186. * @access public
  187. * @param $mode integer
  188. * @return void
  189. */
  190. public static function _isSupportedMode($mode) {
  191. return in_array($mode, Crypt::supportedModes());
  192. }
  193.  
  194. // }}}
  195. // {{{ protected function _setKey()
  196.  
  197. /**
  198. * Set the key used in the cryptography method.
  199. *
  200. * @name _setMode()
  201. * @access protected
  202. * @param $key string
  203. * @return void
  204. */
  205. protected function _setKey($key) {
  206. $this->_key = (string) $key;
  207. }
  208.  
  209. // }}}
  210. // {{{ protected function _setMode()
  211.  
  212. /**
  213. * Set the current returning mode of the class.
  214. *
  215. * @name _setMode()
  216. * @access protected
  217. * @param $mode integer
  218. * @return void
  219. */
  220. protected function _setMode($mode) {
  221. Crypt::_isSupportedMode($mode)
  222. && ($this->_mode = (int)$mode);
  223. }
  224.  
  225. // }}}
  226. // {{{ protected function _encodeHexadecimal()
  227.  
  228. /**
  229. * Encode the data using hexadecimal chars.
  230. *
  231. * @name _encodeHexadecimal()
  232. * @access protected
  233. * @param $data mixed
  234. * @return string
  235. */
  236. protected function _encodeHexadecimal($data) {
  237. $data = (string) $data;
  238. for ($i=0;$i<strlen($data);$i++)
  239. @$hexcrypt .= str_pad(dechex(ord(
  240. $data[$i])), 2, 0, STR_PAD_LEFT);
  241. return @$hexcrypt;
  242. }
  243.  
  244. // }}}
  245. // {{{ protected function _decodeHexadecimal()
  246.  
  247. /**
  248. * Decode hexadecimal strings.
  249. *
  250. * @name _decodeHexadecimal()
  251. * @access protected
  252. * @param $data string
  253. * @return string
  254. */
  255. protected function _decodeHexadecimal($hexcrypt) {
  256. $hexcrypt = (string) $hexcrypt;
  257. for ($i=0;$i<strlen($hexcrypt);$i+=2)
  258. @$data .= chr(hexdec(substr($hexcrypt, $i, 2)));
  259. return @$data;
  260. }
  261.  
  262. // }}}
  263.  
  264. }
  265.  
  266. // }}}
  267. ?>