Source for file crypt.php
Documentation is available at crypt.php
* @package Joomla.Platform
* @copyright Copyright (C) 2005 - 2011 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
* JCrypt is a Joomla Platform class for handling basic encryption/decryption of data.
* @package Joomla.Platform
* @var JCryptCipher The encryption cipher object.
* @var JCryptKey The encryption key[/pair)].
* Object Constructor takes an optional key to be used for encryption/decryption. If no key is given then the
* secret word from the configuration object is used.
* @param JCryptCipher $cipher The encryption cipher object.
* @param JCryptKey $key The encryption key[/pair)].
public function __construct(JCryptCipher $cipher =
null, JCryptKey $key =
null)
// Set the encryption key[/pair)].
// Set the encryption cipher.
* Method to decrypt a data string.
* @param string $data The encrypted string to decrypt.
* @return string The decrypted data string.
* @throws InvalidArgumentException
return $this->_cipher->decrypt($data, $this->_key);
catch
(InvalidArgumentException $e)
* Method to encrypt a data string.
* @param string $data The data string to encrypt.
* @return string The encrypted data string.
return $this->_cipher->encrypt($data, $this->_key);
* Method to generate a new encryption key[/pair] object.
* @param array $options Key generation options.
* Method to set the encryption key[/pair] object.
* @param JCryptKey $key The key object to set.
public function setKey(JCryptKey $key)
* @param integer $length Length of the random data to generate
* @return string Random binary data
* If a secure randomness generator exists and we don't
* have a buggy PHP version use it.
* Collect any entropy available in the system along with a number
* of time measurements of operating system randomness.
// Check if we can use /dev/urandom.
// This is PHP 5.3.3 and up
$handle =
@fopen('/dev/urandom', 'rb');
while ($length >
strlen($randomStr))
$bytes =
($total >
$shaHashLength)?
$shaHashLength :
$total;
* Collect any entropy available from the PHP system and filesystem.
* If we have ssl data that isn't strong, we use it once.
$entropy .=
@fread($handle, $bytes);
* There is no external source of entropy so we repeat calls
* to mt_rand until we are assured there's real randomness in
* Measure the time that the operations will take on average.
for ($pass =
0; $pass <
$samples; ++
$pass)
for ($count =
0; $count <
50; ++
$count)
$hash =
sha1($hash, true);
$entropy .=
$microStart .
$microEnd;
if ($microStart >=
$microEnd)
$duration +=
$microEnd -
$microStart;
$duration =
$duration /
$samples;
* Based on the average time, determine the total rounds so that
* the total running time is bounded to a reasonable number.
$rounds = (int)
(($maxTimeMicro /
$duration) *
50);
* Take additional measurements. On average we can expect
* at least $bitsPerRound bits of entropy from each measurement.
$iter =
$bytes * (int)
ceil(8 /
$bitsPerRound);
for ($pass =
0; $pass <
$iter; ++
$pass)
for ($count =
0; $count <
$rounds; ++
$count)
$hash =
sha1($hash, true);
$randomStr .=
sha1($entropy, true);
return substr($randomStr, 0, $length);
* A timing safe comparison method. This defeats hacking
* attempts that use timing based attack vectors.
* @param string $known A known string to check against.
* @param string $unknown An unknown string to check.
* @return boolean True if the two strings are exactly the same.
// Prevent issues if string length is 0
$knownLength =
strlen($known);
$unknownLength =
strlen($unknown);
// Set the result to the difference between the lengths
$result =
$knownLength -
$unknownLength;
// Note that we ALWAYS iterate over the user-supplied length to prevent leaking length info.
for ($i =
0; $i <
$unknownLength; $i++
)
// Using % here is a trick to prevent notices. It's safe, since if the lengths are different, $result is already non-0
$result |=
(ord($known[$i %
$knownLength]) ^
ord($unknown[$i]));
// They are only identical strings if $result is exactly 0...
* Tests for the availability of updated crypt().
* Based on a method by Anthony Ferrera
* @return boolean True if updated crypt() is available.
* @note To be removed when PHP 5.3.7 or higher is the minimum supported version.
* @see https://github.com/ircmaxell/password_compat/blob/master/version-test.php
// Check to see whether crypt() is supported.
if (version_compare(PHP_VERSION, '5.3.7', '>=') ===
true)
// We have safe PHP version.
// We need to test if we have patched PHP version.
jimport('compat.password.lib.version_test');
$pass =
$test->version_test();
if ($pass &&
!defined('PASSWORD_DEFAULT'))
// Always make sure that the password hashing API has been defined.
include_once JPATH_ROOT .
'/libraries/compat/password/lib/password.php';
Documentation generated on Tue, 19 Nov 2013 14:57:39 +0100 by phpDocumentor 1.4.3