10 defined(
'JPATH_PLATFORM') or die;
61 public function decrypt($data)
65 return $this->_cipher->
decrypt($data, $this->_key);
67 catch (InvalidArgumentException $e)
82 public function encrypt($data)
84 return $this->_cipher->encrypt($data, $this->_key);
96 public function generateKey(array $options = array())
98 return $this->_cipher->generateKey($options);
126 public static function genRandomBytes($length = 16)
128 $length = (int) $length;
135 if (function_exists(
'openssl_random_pseudo_bytes')
136 && (version_compare(PHP_VERSION,
'5.3.4') >= 0 || IS_WIN))
138 $sslStr = openssl_random_pseudo_bytes($length, $strong);
161 if (function_exists(
'stream_set_read_buffer') && @is_readable(
'/dev/urandom'))
163 $handle = @fopen(
'/dev/urandom',
'rb');
171 while ($length > strlen($randomStr))
173 $bytes = ($total > $shaHashLength)? $shaHashLength : $total;
180 $entropy = rand() . uniqid(mt_rand(),
true) . $sslStr;
181 $entropy .= implode(
'', @fstat(fopen(__FILE__,
'r')));
182 $entropy .= memory_get_usage();
187 stream_set_read_buffer($handle, 0);
188 $entropy .= @fread($handle, $bytes);
202 for ($pass = 0; $pass < $samples; ++$pass)
204 $microStart = microtime(
true) * 1000000;
205 $hash = sha1(mt_rand(),
true);
207 for ($count = 0; $count < 50; ++$count)
209 $hash = sha1($hash,
true);
212 $microEnd = microtime(
true) * 1000000;
213 $entropy .= $microStart . $microEnd;
215 if ($microStart >= $microEnd)
217 $microEnd += 1000000;
220 $duration += $microEnd - $microStart;
223 $duration = $duration / $samples;
229 $rounds = (int) (($maxTimeMicro / $duration) * 50);
235 $iter = $bytes * (int) ceil(8 / $bitsPerRound);
237 for ($pass = 0; $pass < $iter; ++$pass)
239 $microStart = microtime(
true);
240 $hash = sha1(mt_rand(),
true);
242 for ($count = 0; $count < $rounds; ++$count)
244 $hash = sha1($hash,
true);
247 $entropy .= $microStart . microtime(
true);
251 $randomStr .= sha1($entropy,
true);
259 return substr($randomStr, 0, $length);
273 public static function timingSafeCompare($known, $unknown)
279 $knownLength = strlen($known);
280 $unknownLength = strlen($unknown);
283 $result = $knownLength - $unknownLength;
286 for ($i = 0; $i < $unknownLength; $i++)
289 $result |= (ord($known[$i % $knownLength]) ^ ord($unknown[$i]));
293 return $result === 0;
306 public static function hasStrongPasswordSupport()
313 if (version_compare(PHP_VERSION,
'5.3.7',
'>=') ===
true)
321 jimport(
'compat.password.lib.version_test');
326 if ($pass && !defined(
'PASSWORD_DEFAULT'))
329 include_once JPATH_ROOT .
'/libraries/compat/password/lib/password.php';