Fixing content type ordering when content_type is not defined.
[akelos.git] / vendor / pear / Crypt / Blowfish.php
bloba7b8948f043679ce6c5870438d82ae4a4a6ba7ff
1 <?php
2 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4 /**
5 * Crypt_Blowfish allows for encryption and decryption on the fly using
6 * the Blowfish algorithm. Crypt_Blowfish does not require the mcrypt
7 * PHP extension, it uses only PHP.
8 * Crypt_Blowfish support encryption/decryption with or without a secret key.
11 * PHP versions 4 and 5
13 * LICENSE: This source file is subject to version 3.0 of the PHP license
14 * that is available through the world-wide-web at the following URI:
15 * http://www.php.net/license/3_0.txt. If you did not receive a copy of
16 * the PHP License and are unable to obtain it through the web, please
17 * send a note to license@php.net so we can mail you a copy immediately.
19 * @category Encryption
20 * @package Crypt_Blowfish
21 * @author Matthew Fonda <mfonda@php.net>
22 * @copyright 2005 Matthew Fonda
23 * @license http://www.php.net/license/3_0.txt PHP License 3.0
24 * @version CVS: $Id: Blowfish.php,v 1.81 2005/05/30 18:40:36 mfonda Exp $
25 * @link http://pear.php.net/package/Crypt_Blowfish
29 require_once 'PEAR.php';
32 /**
34 * Example usage:
35 * $bf = new Crypt_Blowfish('some secret key!');
36 * $encrypted = $bf->encrypt('this is some example plain text');
37 * $plaintext = $bf->decrypt($encrypted);
38 * echo "plain text: $plaintext";
41 * @category Encryption
42 * @package Crypt_Blowfish
43 * @author Matthew Fonda <mfonda@php.net>
44 * @copyright 2005 Matthew Fonda
45 * @license http://www.php.net/license/3_0.txt PHP License 3.0
46 * @link http://pear.php.net/package/Crypt_Blowfish
47 * @version @package_version@
48 * @access public
50 class Crypt_Blowfish
52 /**
53 * P-Array contains 18 32-bit subkeys
55 * @var array
56 * @access private
58 var $_P = array();
61 /**
62 * Array of four S-Blocks each containing 256 32-bit entries
64 * @var array
65 * @access private
67 var $_S = array();
69 /**
70 * Mcrypt td resource
72 * @var resource
73 * @access private
75 var $_td = null;
77 /**
78 * Initialization vector
80 * @var string
81 * @access private
83 var $_iv = null;
86 /**
87 * Crypt_Blowfish Constructor
88 * Initializes the Crypt_Blowfish object, and gives a sets
89 * the secret key
91 * @param string $key
92 * @access public
94 function Crypt_Blowfish($key)
96 if (extension_loaded('mcrypt')) {
97 $this->_td = mcrypt_module_open(MCRYPT_BLOWFISH, '', 'ecb', '');
98 $this->_iv = mcrypt_create_iv(8, MCRYPT_RAND);
100 $this->setKey($key);
104 * Deprecated isReady method
106 * @return bool
107 * @access public
108 * @deprecated
110 function isReady()
112 return true;
116 * Deprecated init method - init is now a private
117 * method and has been replaced with _init
119 * @return bool
120 * @access public
121 * @deprecated
122 * @see Crypt_Blowfish::_init()
124 function init()
126 $this->_init();
130 * Initializes the Crypt_Blowfish object
132 * @access private
134 function _init()
136 $defaults = new Crypt_Blowfish_DefaultKey();
137 $this->_P = $defaults->P;
138 $this->_S = $defaults->S;
142 * Enciphers a single 64 bit block
144 * @param int &$Xl
145 * @param int &$Xr
146 * @access private
148 function _encipher(&$Xl, &$Xr)
150 for ($i = 0; $i < 16; $i++) {
151 $temp = $Xl ^ $this->_P[$i];
152 $Xl = ((($this->_S[0][($temp>>24) & 255] +
153 $this->_S[1][($temp>>16) & 255]) ^
154 $this->_S[2][($temp>>8) & 255]) +
155 $this->_S[3][$temp & 255]) ^ $Xr;
156 $Xr = $temp;
158 $Xr = $Xl ^ $this->_P[16];
159 $Xl = $temp ^ $this->_P[17];
164 * Deciphers a single 64 bit block
166 * @param int &$Xl
167 * @param int &$Xr
168 * @access private
170 function _decipher(&$Xl, &$Xr)
172 for ($i = 17; $i > 1; $i--) {
173 $temp = $Xl ^ $this->_P[$i];
174 $Xl = ((($this->_S[0][($temp>>24) & 255] +
175 $this->_S[1][($temp>>16) & 255]) ^
176 $this->_S[2][($temp>>8) & 255]) +
177 $this->_S[3][$temp & 255]) ^ $Xr;
178 $Xr = $temp;
180 $Xr = $Xl ^ $this->_P[1];
181 $Xl = $temp ^ $this->_P[0];
186 * Encrypts a string
188 * @param string $plainText
189 * @return string Returns cipher text on success, PEAR_Error on failure
190 * @access public
192 function encrypt($plainText)
194 if (!is_string($plainText)) {
195 PEAR::raiseError('Plain text must be a string', 0, PEAR_ERROR_DIE);
198 if (extension_loaded('mcrypt')) {
199 return mcrypt_generic($this->_td, $plainText);
202 $cipherText = '';
203 $len = strlen($plainText);
204 $plainText .= str_repeat(chr(0),(8 - ($len%8))%8);
205 for ($i = 0; $i < $len; $i += 8) {
206 list(,$Xl,$Xr) = unpack("N2",substr($plainText,$i,8));
207 $this->_encipher($Xl, $Xr);
208 $cipherText .= pack("N2", $Xl, $Xr);
210 return $cipherText;
215 * Decrypts an encrypted string
217 * @param string $cipherText
218 * @return string Returns plain text on success, PEAR_Error on failure
219 * @access public
221 function decrypt($cipherText)
223 if (!is_string($cipherText)) {
224 PEAR::raiseError('Chiper text must be a string', 1, PEAR_ERROR_DIE);
227 if (extension_loaded('mcrypt')) {
228 return mdecrypt_generic($this->_td, $cipherText);
231 $plainText = '';
232 $len = strlen($cipherText);
233 $cipherText .= str_repeat(chr(0),(8 - ($len%8))%8);
234 for ($i = 0; $i < $len; $i += 8) {
235 list(,$Xl,$Xr) = unpack("N2",substr($cipherText,$i,8));
236 $this->_decipher($Xl, $Xr);
237 $plainText .= pack("N2", $Xl, $Xr);
239 return $plainText;
244 * Sets the secret key
245 * The key must be non-zero, and less than or equal to
246 * 56 characters in length.
248 * @param string $key
249 * @return bool Returns true on success, PEAR_Error on failure
250 * @access public
252 function setKey($key)
254 if (!is_string($key)) {
255 PEAR::raiseError('Key must be a string', 2, PEAR_ERROR_DIE);
258 $len = strlen($key);
260 if ($len > 56 || $len == 0) {
261 PEAR::raiseError('Key must be less than 56 characters and non-zero. Supplied key length: ' . $len, 3, PEAR_ERROR_DIE);
264 if (extension_loaded('mcrypt')) {
265 mcrypt_generic_init($this->_td, $key, $this->_iv);
266 return true;
269 require_once 'Blowfish/DefaultKey.php';
270 $this->_init();
272 $k = 0;
273 $data = 0;
274 $datal = 0;
275 $datar = 0;
277 for ($i = 0; $i < 18; $i++) {
278 $data = 0;
279 for ($j = 4; $j > 0; $j--) {
280 $data = $data << 8 | ord($key{$k});
281 $k = ($k+1) % $len;
283 $this->_P[$i] ^= $data;
286 for ($i = 0; $i <= 16; $i += 2) {
287 $this->_encipher($datal, $datar);
288 $this->_P[$i] = $datal;
289 $this->_P[$i+1] = $datar;
291 for ($i = 0; $i < 256; $i += 2) {
292 $this->_encipher($datal, $datar);
293 $this->_S[0][$i] = $datal;
294 $this->_S[0][$i+1] = $datar;
296 for ($i = 0; $i < 256; $i += 2) {
297 $this->_encipher($datal, $datar);
298 $this->_S[1][$i] = $datal;
299 $this->_S[1][$i+1] = $datar;
301 for ($i = 0; $i < 256; $i += 2) {
302 $this->_encipher($datal, $datar);
303 $this->_S[2][$i] = $datal;
304 $this->_S[2][$i+1] = $datar;
306 for ($i = 0; $i < 256; $i += 2) {
307 $this->_encipher($datal, $datar);
308 $this->_S[3][$i] = $datal;
309 $this->_S[3][$i+1] = $datar;
312 return true;