PageRenderTime 60ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/application/libraries/PEAR/Crypt/Blowfish.php

https://github.com/grandison/budo16
PHP | 317 lines | 90 code | 25 blank | 202 comment | 10 complexity | c4e657b93cfc22c87c96a4a875800fb1 MD5 | raw file
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3. /**
  4. * Crypt_Blowfish allows for encryption and decryption on the fly using
  5. * the Blowfish algorithm. Crypt_Blowfish does not require the MCrypt
  6. * PHP extension, but uses it if available, otherwise it uses only PHP.
  7. * Crypt_Blowfish supports encryption/decryption with or without a secret key.
  8. *
  9. * PHP versions 4 and 5
  10. *
  11. * @category Encryption
  12. * @package Crypt_Blowfish
  13. * @author Matthew Fonda <mfonda@php.net>
  14. * @copyright 2005-2008 Matthew Fonda
  15. * @license http://www.opensource.net/licenses/bsd-license.php New BSD
  16. * @version CVS: $Id: Blowfish.php,v 1.86 2008/08/30 21:53:50 jausions Exp $
  17. * @link http://pear.php.net/package/Crypt_Blowfish
  18. */
  19. /**
  20. * Required PEAR package(s)
  21. */
  22. require_once 'PEAR.php';
  23. /**
  24. * Engine choice constants
  25. */
  26. /**
  27. * To let the Crypt_Blowfish package decide which engine to use
  28. * @since 1.1.0
  29. */
  30. define('CRYPT_BLOWFISH_AUTO', 1);
  31. /**
  32. * To use the MCrypt PHP extension.
  33. * @since 1.1.0
  34. */
  35. define('CRYPT_BLOWFISH_MCRYPT', 2);
  36. /**
  37. * To use the PHP-only engine.
  38. * @since 1.1.0
  39. */
  40. define('CRYPT_BLOWFISH_PHP', 3);
  41. /**
  42. * Example using the factory method in CBC mode
  43. *
  44. * <code>
  45. * $bf =& Crypt_Blowfish::factory('cbc');
  46. * if (PEAR::isError($bf)) {
  47. * echo $bf->getMessage();
  48. * exit;
  49. * }
  50. * $iv = 'abc123+=';
  51. * $key = 'My secret key';
  52. * $bf->setKey($key, $iv);
  53. * $encrypted = $bf->encrypt('this is some example plain text');
  54. * $bf->setKey($key, $iv);
  55. * $plaintext = $bf->decrypt($encrypted);
  56. * if (PEAR::isError($plaintext)) {
  57. * echo $plaintext->getMessage();
  58. * exit;
  59. * }
  60. * // Encrypted text is padded prior to encryption
  61. * // so you may need to trim the decrypted result.
  62. * echo 'plain text: ' . trim($plaintext);
  63. * </code>
  64. *
  65. * To disable using the mcrypt library, define the CRYPT_BLOWFISH_NOMCRYPT
  66. * constant. This is useful for instance on Windows platform with a buggy
  67. * mdecrypt_generic() function.
  68. * <code>
  69. * define('CRYPT_BLOWFISH_NOMCRYPT', true);
  70. * </code>
  71. *
  72. * @category Encryption
  73. * @package Crypt_Blowfish
  74. * @author Matthew Fonda <mfonda@php.net>
  75. * @author Philippe Jausions <jausions@php.net>
  76. * @copyright 2005-2008 Matthew Fonda
  77. * @license http://www.opensource.net/licenses/bsd-license.php New BSD
  78. * @link http://pear.php.net/package/Crypt_Blowfish
  79. * @version 1.1.0RC2
  80. * @access public
  81. */
  82. class Crypt_Blowfish
  83. {
  84. /**
  85. * Implementation-specific Crypt_Blowfish object
  86. *
  87. * @var object
  88. * @access private
  89. */
  90. var $_crypt = null;
  91. /**
  92. * Initialization vector
  93. *
  94. * @var string
  95. * @access protected
  96. */
  97. var $_iv = null;
  98. /**
  99. * Holds block size
  100. *
  101. * @var integer
  102. * @access protected
  103. */
  104. var $_block_size = 8;
  105. /**
  106. * Holds IV size
  107. *
  108. * @var integer
  109. * @access protected
  110. */
  111. var $_iv_size = 8;
  112. /**
  113. * Holds max key size
  114. *
  115. * @var integer
  116. * @access protected
  117. */
  118. var $_key_size = 56;
  119. /**
  120. * Crypt_Blowfish Constructor
  121. * Initializes the Crypt_Blowfish object (in EBC mode), and sets
  122. * the secret key
  123. *
  124. * @param string $key
  125. * @access public
  126. * @deprecated Since 1.1.0
  127. * @see Crypt_Blowfish::factory()
  128. */
  129. function Crypt_Blowfish($key)
  130. {
  131. $this->_crypt =& Crypt_Blowfish::factory('ecb', $key);
  132. if (!PEAR::isError($this->_crypt)) {
  133. $this->_crypt->setKey($key);
  134. }
  135. }
  136. /**
  137. * Crypt_Blowfish object factory
  138. *
  139. * This is the recommended method to create a Crypt_Blowfish instance.
  140. *
  141. * When using CRYPT_BLOWFISH_AUTO, you can force the package to ignore
  142. * the MCrypt extension, by defining CRYPT_BLOWFISH_NOMCRYPT.
  143. *
  144. * @param string $mode operating mode 'ecb' or 'cbc' (case insensitive)
  145. * @param string $key
  146. * @param string $iv initialization vector (must be provided for CBC mode)
  147. * @param integer $engine one of CRYPT_BLOWFISH_AUTO, CRYPT_BLOWFISH_PHP
  148. * or CRYPT_BLOWFISH_MCRYPT
  149. * @return object Crypt_Blowfish object or PEAR_Error object on error
  150. * @access public
  151. * @static
  152. * @since 1.1.0
  153. */
  154. function &factory($mode = 'ecb', $key = null, $iv = null, $engine = CRYPT_BLOWFISH_AUTO)
  155. {
  156. switch ($engine) {
  157. case CRYPT_BLOWFISH_AUTO:
  158. if (!defined('CRYPT_BLOWFISH_NOMCRYPT')
  159. && extension_loaded('mcrypt')) {
  160. $engine = CRYPT_BLOWFISH_MCRYPT;
  161. } else {
  162. $engine = CRYPT_BLOWFISH_PHP;
  163. }
  164. break;
  165. case CRYPT_BLOWFISH_MCRYPT:
  166. if (!PEAR::loadExtension('mcrypt')) {
  167. return PEAR::raiseError('MCrypt extension is not available.');
  168. }
  169. break;
  170. }
  171. switch ($engine) {
  172. case CRYPT_BLOWFISH_PHP:
  173. $mode = strtoupper($mode);
  174. $class = 'Crypt_Blowfish_' . $mode;
  175. include_once 'Crypt/Blowfish/' . $mode . '.php';
  176. $crypt = new $class(null);
  177. break;
  178. case CRYPT_BLOWFISH_MCRYPT:
  179. include_once 'Crypt/Blowfish/MCrypt.php';
  180. $crypt = new Crypt_Blowfish_MCrypt(null, $mode);
  181. break;
  182. }
  183. if (!is_null($key) || !is_null($iv)) {
  184. $result = $crypt->setKey($key, $iv);
  185. if (PEAR::isError($result)) {
  186. return $result;
  187. }
  188. }
  189. return $crypt;
  190. }
  191. /**
  192. * Returns the algorithm's block size
  193. *
  194. * @return integer
  195. * @access public
  196. * @since 1.1.0
  197. */
  198. function getBlockSize()
  199. {
  200. return $this->_block_size;
  201. }
  202. /**
  203. * Returns the algorithm's IV size
  204. *
  205. * @return integer
  206. * @access public
  207. * @since 1.1.0
  208. */
  209. function getIVSize()
  210. {
  211. return $this->_iv_size;
  212. }
  213. /**
  214. * Returns the algorithm's maximum key size
  215. *
  216. * @return integer
  217. * @access public
  218. * @since 1.1.0
  219. */
  220. function getMaxKeySize()
  221. {
  222. return $this->_key_size;
  223. }
  224. /**
  225. * Deprecated isReady method
  226. *
  227. * @return bool
  228. * @access public
  229. * @deprecated
  230. */
  231. function isReady()
  232. {
  233. return true;
  234. }
  235. /**
  236. * Deprecated init method - init is now a private
  237. * method and has been replaced with _init
  238. *
  239. * @return bool
  240. * @access public
  241. * @deprecated
  242. */
  243. function init()
  244. {
  245. return $this->_crypt->init();
  246. }
  247. /**
  248. * Encrypts a string
  249. *
  250. * Value is padded with NUL characters prior to encryption. You may
  251. * need to trim or cast the type when you decrypt.
  252. *
  253. * @param string $plainText the string of characters/bytes to encrypt
  254. * @return string|PEAR_Error Returns cipher text on success, PEAR_Error on failure
  255. * @access public
  256. */
  257. function encrypt($plainText)
  258. {
  259. return $this->_crypt->encrypt($plainText);
  260. }
  261. /**
  262. * Decrypts an encrypted string
  263. *
  264. * The value was padded with NUL characters when encrypted. You may
  265. * need to trim the result or cast its type.
  266. *
  267. * @param string $cipherText the binary string to decrypt
  268. * @return string|PEAR_Error Returns plain text on success, PEAR_Error on failure
  269. * @access public
  270. */
  271. function decrypt($cipherText)
  272. {
  273. return $this->_crypt->decrypt($cipherText);
  274. }
  275. /**
  276. * Sets the secret key
  277. * The key must be non-zero, and less than or equal to
  278. * 56 characters (bytes) in length.
  279. *
  280. * If you are making use of the PHP MCrypt extension, you must call this
  281. * method before each encrypt() and decrypt() call.
  282. *
  283. * @param string $key
  284. * @return boolean|PEAR_Error Returns TRUE on success, PEAR_Error on failure
  285. * @access public
  286. */
  287. function setKey($key)
  288. {
  289. return $this->_crypt->setKey($key);
  290. }
  291. }
  292. ?>