Exemplo n.º 1
0
function _key($v, $arg)
{
    echo '  key: ' . $v;
    if (count($arg) > 0) {
        echo "\n\n  Arguments:";
        foreach ($arg as $a) {
            echo "\n\t{$a}";
        }
    }
    if ($v == 'generate') {
        if (!is_dir(CONFIG_KEYS_PATH)) {
            mkdir(CONFIG_KEYS_PATH, 0777);
        }
        $base = ['I', 'u', 'h', '5', 'B', 'A', 'r', 'i', '7', '9', 'z', 'd', 'n', 't', 'F', '2', 'W', 'X', 'f', 'e', 'x', 'v', '_', '8', 'm', 'T', 'N', 'R', 'L', 'c', '6', 'P', 'k', 'Q', 'q', 'j', 'Y', 'M', '4', 'S', 'G', 'o', '0', '$', 'K', 's', 'g', 'H', 'E', 'b', 'a', 'J', 'U', 'Z', 'l', '1', 'O', '3', 'y', 'p', 'V', 'D', 'C', 'w'];
        $extra = ['$', '!', '#', '%', '&', '*', '+', '-', '?', '@', '(', ')', '/', '\\', '[', ']', '_', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];
        shuffle($base);
        shuffle($extra);
        file_put_contents(CONFIG_KEYS_PATH . 'can.key', implode($base) . "\n" . implode($extra));
        echo "\n\n  New CAN key generated - success!";
        //Now, OPEN_SSL
        include CLI_PATH . 'open.php';
        return "\n\n  OpenSSL keys & certificates - success!";
    } elseif ($v == 'list') {
        echo "\n\n  Ciphers:";
        foreach (mcrypt_list_algorithms() as $x) {
            echo "\n\t" . $x;
        }
        echo "\n\n  Cipher Modes:";
        foreach (mcrypt_list_modes() as $x) {
            echo "\n\t" . $x;
        }
    } else {
        return "\n\n  ----- ERROR: Command 'key:{$v}' not found!\n" . _help();
    }
}
 public static function get_system_requirements()
 {
     return [['name' => 'Memory limit', 'expected_value' => '640M', 'current_value' => function () {
         return ini_get('memory_limit');
     }, 'check' => function ($current_value, $expected_value) {
         return intval($current_value) >= intval($expected_value);
     }], ['name' => 'Max execution time', 'expected_value' => '30', 'current_value' => function () {
         return ini_get('max_execution_time');
     }, 'check' => function ($current_value, $expected_value) {
         return intval($current_value) >= intval($expected_value);
     }], ['name' => 'cUrl enabled', 'expected_value' => 'Yes', 'current_value' => function () {
         return extension_loaded('curl') ? 'Yes' : 'No';
     }, 'check' => function ($current_value, $expected_value) {
         return $current_value == 'Yes';
     }], ['name' => 'mCrypt enabled', 'expected_value' => 'Yes', 'current_value' => function () {
         return extension_loaded('mcrypt') ? 'Yes' : 'No';
     }, 'check' => function ($current_value, $expected_value) {
         return $current_value == 'Yes';
     }], ['name' => 'RIJNDAEL 128 available', 'expected_value' => 'Yes', 'current_value' => function () {
         return extension_loaded('mcrypt') && in_array('rijndael-128', mcrypt_list_algorithms()) ? 'Yes' : 'No';
     }, 'check' => function ($current_value, $expected_value) {
         return $current_value == 'Yes';
     }], ['name' => 'CBC mode available', 'expected_value' => 'Yes', 'current_value' => function () {
         return extension_loaded('mcrypt') && in_array('cbc', mcrypt_list_modes()) ? 'Yes' : 'No';
     }, 'check' => function ($current_value, $expected_value) {
         return $current_value == 'Yes';
     }], ['name' => 'SHA512 available', 'expected_value' => 'Yes', 'current_value' => function () {
         return in_array('sha512', hash_algos()) ? 'Yes' : 'No';
     }, 'check' => function ($current_value, $expected_value) {
         return $current_value == 'Yes';
     }]];
 }
function encryptalgo($config)
{
    if (@function_exists('mcrypt_list_algorithms')) {
        $listed = array();
        if (!isset($config['mcrypt_algo'])) {
            $config['mcrypt_algo'] = 'tripledes';
            /* MCRYPT_TRIPLEDES */
        }
        $algos = @mcrypt_list_algorithms();
        $found = False;
        while (list($key, $value) = each($algos)) {
            $found = True;
            /* Only show each once - seems this is a problem in some installs */
            if (!in_array($value, $listed)) {
                if ($config['mcrypt_algo'] == $value) {
                    $selected = ' selected';
                } else {
                    $selected = '';
                }
                $descr = strtoupper($value);
                $out .= '<option value="' . $value . '"' . $selected . '>' . $descr . '</option>' . "\n";
                $listed[] = $value;
            }
        }
        if (!$found) {
            /* Something is wrong with their mcrypt install or php.ini */
            $out = '<option value="">' . lang('no algorithms available') . '</option>' . "\n";
        }
    } else {
        $out = '<option value="tripledes">TRIPLEDES</option>' . "\n";
    }
    return $out;
}
Exemplo n.º 4
0
 public function setAlgo($algo)
 {
     $algorithms = mcrypt_list_algorithms("/usr/local/lib/libmcrypt");
     if (in_array($algo, $algorithms)) {
         $this->algo = $algo;
     }
 }
Exemplo n.º 5
0
 public function Download($link)
 {
     if (!extension_loaded('mcrypt') || !in_array('rijndael-128', mcrypt_list_algorithms(), true)) {
         html_error("Mcrypt module isn't installed or it doesn't have support for the needed encryption.");
     }
     $this->RLCheck();
     $this->seqno = mt_rand();
     $this->changeMesg(lang(300) . '<br />Mega.co.nz plugin by Th3-822');
     // Please, do not remove or change this line contents. - Th3-822
     $fragment = parse_url($link, PHP_URL_FRAGMENT);
     if (preg_match('@^F!([^!]{8})!([\\w\\-\\,]{22})@i', $fragment, $fid)) {
         return $this->Folder($fid[1], $fid[2]);
     }
     if (!preg_match('@^(T8)?!([^!]{8})!([\\w\\-\\,]{43})@i', $fragment, $fid)) {
         html_error('FileID or Key not found at link.');
     }
     $reply = $this->apiReq(array('a' => 'g', 'g' => '1', empty($fid[1]) ? 'p' : 'n' => $fid[2], 'ssl' => '0'));
     $this->CheckErr($reply[0]);
     if (!empty($reply[0]['e'])) {
         $this->CheckErr($reply[0]['e']);
     }
     $key = $this->base64_to_a32($fid[3]);
     $iv = array_merge(array_slice($key, 4, 2), array(0, 0));
     $key = array($key[0] ^ $key[4], $key[1] ^ $key[5], $key[2] ^ $key[6], $key[3] ^ $key[7]);
     $attr = $this->dec_attr($this->base64url_decode($reply[0]['at']), $key);
     if (empty($attr)) {
         html_error((!empty($fid[1]) ? 'Folder Error: ' : '') . 'File\'s key isn\'t correct.');
     }
     $this->RedirectDownload($reply[0]['g'], $attr['n'], 0, 0, $link, 0, 0, array('T8[fkey]' => $fid[3]));
 }
Exemplo n.º 6
0
 static function IsMcryptAvailable()
 {
     if (!is_bool(self::$mcryptavailable)) {
         self::$mcryptavailable = function_exists("mcrypt_module_open") && in_array("rijndael-128", mcrypt_list_algorithms());
     }
     return self::$mcryptavailable;
 }
Exemplo n.º 7
0
/**
 * Get selectbox for supported encryption algorithms selectbox
 *
 * @param $config
 * @return string HTML code for encryption algorithm selection
 */
function encryptalgo($config)
{
    if (function_exists('mcrypt_list_algorithms')) {
        $listed = array();
        if (!isset($config['mcrypt_algo'])) {
            $config['mcrypt_algo'] = MCRYPT_TRIPLEDES;
        }
        $algos = mcrypt_list_algorithms();
        $found = False;
        $out = '';
        foreach ($algos as $algo) {
            $found = True;
            /* Only show each once - seems this is a problem in some installs */
            if (in_array($algo, $listed)) {
                continue;
            }
            $selected = '';
            if ($config['mcrypt_algo'] == $algo) {
                $selected = ' selected';
            }
            $descr = strtoupper($algo);
            $out .= "<option value=\"{$algo}\"{$selected}>{$descr}</option>\n";
            $listed[] = $algo;
        }
        if (!$found) {
            /* Something is wrong with their mcrypt install or php.ini */
            $out = '<option value="">' . lang('no algorithms available') . '</option>' . "\n";
        }
    } else {
        $out = '<option value="tripledes">TRIPLEDES</option>' . "\n";
    }
    return $out;
}
Exemplo n.º 8
0
 public function Crypt_AES($mode)
 {
     if (!defined("CRYPT_AES_MODE")) {
         switch (true) {
             case extension_loaded("mcrypt") && in_array("rijndael-128", mcrypt_list_algorithms()):
                 define("CRYPT_AES_MODE", CRYPT_AES_MODE_MCRYPT);
                 break;
             default:
                 define("CRYPT_AES_MODE", CRYPT_AES_MODE_INTERNAL);
         }
     }
     switch (CRYPT_AES_MODE) {
         case CRYPT_AES_MODE_MCRYPT:
             switch ($mode) {
                 case CRYPT_AES_MODE_ECB:
                     $this->paddable = true;
                     $this->mode = MCRYPT_MODE_ECB;
                     break;
                 case CRYPT_AES_MODE_CTR:
                     $this->mode = "ctr";
                     break;
                 case CRYPT_AES_MODE_CFB:
                     $this->mode = "ncfb";
                     break;
                 case CRYPT_AES_MODE_OFB:
                     $this->mode = MCRYPT_MODE_NOFB;
                     break;
                 case CRYPT_AES_MODE_CBC:
                 default:
                     $this->paddable = true;
                     $this->mode = MCRYPT_MODE_CBC;
             }
             break;
         default:
             switch ($mode) {
                 case CRYPT_AES_MODE_ECB:
                     $this->paddable = true;
                     $this->mode = CRYPT_RIJNDAEL_MODE_ECB;
                     break;
                 case CRYPT_AES_MODE_CTR:
                     $this->mode = CRYPT_RIJNDAEL_MODE_CTR;
                     break;
                 case CRYPT_AES_MODE_CFB:
                     $this->mode = CRYPT_RIJNDAEL_MODE_CFB;
                     break;
                 case CRYPT_AES_MODE_OFB:
                     $this->mode = CRYPT_RIJNDAEL_MODE_OFB;
                     break;
                 case CRYPT_AES_MODE_CBC:
                 default:
                     $this->paddable = true;
                     $this->mode = CRYPT_RIJNDAEL_MODE_CBC;
             }
     }
     CRYPT_AES_MODE;
     if (CRYPT_AES_MODE == CRYPT_AES_MODE_INTERNAL) {
         parent::Crypt_Rijndael($this->mode);
     }
 }
Exemplo n.º 9
0
 /**
  * @return array
  */
 public static function listAlgorithms()
 {
     if (!self::$listAlgorithms) {
         $algorithms = mcrypt_list_algorithms();
         self::$listAlgorithms = array_combine($algorithms, $algorithms);
     }
     return self::$listAlgorithms;
 }
Exemplo n.º 10
0
 public function Crypt_AES($mode = CRYPT_AES_MODE_CBC)
 {
     if (!defined('CRYPT_AES_MODE')) {
         switch (true) {
             case extension_loaded('mcrypt') && in_array('rijndael-128', mcrypt_list_algorithms()):
                 define('CRYPT_AES_MODE', CRYPT_AES_MODE_MCRYPT);
                 break;
             default:
                 define('CRYPT_AES_MODE', CRYPT_AES_MODE_INTERNAL);
         }
     }
     switch (CRYPT_AES_MODE) {
         case CRYPT_AES_MODE_MCRYPT:
             switch ($mode) {
                 case CRYPT_AES_MODE_ECB:
                     $this->paddable = true;
                     $this->mode = MCRYPT_MODE_ECB;
                     break;
                 case CRYPT_AES_MODE_CTR:
                     $this->mode = 'ctr';
                     break;
                 case CRYPT_AES_MODE_CFB:
                     $this->mode = 'ncfb';
                     break;
                 case CRYPT_AES_MODE_OFB:
                     $this->mode = MCRYPT_MODE_NOFB;
                     break;
                 case CRYPT_AES_MODE_CBC:
                 default:
                     $this->paddable = true;
                     $this->mode = MCRYPT_MODE_CBC;
             }
             break;
         default:
             switch ($mode) {
                 case CRYPT_AES_MODE_ECB:
                     $this->paddable = true;
                     $this->mode = CRYPT_RIJNDAEL_MODE_ECB;
                     break;
                 case CRYPT_AES_MODE_CTR:
                     $this->mode = CRYPT_RIJNDAEL_MODE_CTR;
                     break;
                 case CRYPT_AES_MODE_CFB:
                     $this->mode = CRYPT_RIJNDAEL_MODE_CFB;
                     break;
                 case CRYPT_AES_MODE_OFB:
                     $this->mode = CRYPT_RIJNDAEL_MODE_OFB;
                     break;
                 case CRYPT_AES_MODE_CBC:
                 default:
                     $this->paddable = true;
                     $this->mode = CRYPT_RIJNDAEL_MODE_CBC;
             }
     }
     if (CRYPT_AES_MODE == CRYPT_AES_MODE_INTERNAL) {
         parent::Crypt_Rijndael($this->mode);
     }
 }
 /**
  *
  * @param $algorithm
  * @return Crypt
  */
 public function setAlgorithm($algorithm)
 {
     // Make sure algorythm is available
     if (!in_array($algorithm, mcrypt_list_algorithms())) {
         throw new Exception("MCRYPT Algorithm {$algorithm} is not available.");
     }
     $this->_algorithm = $algorithm;
     return $this;
 }
Exemplo n.º 12
0
 /**
  * Checks the environment for mcrypt and mcrypt module
  *
  * @return void
  * @author Osman Üngür
  */
 private function checkEnvironment()
 {
     if (!extension_loaded('mcrypt') || !function_exists('mcrypt_module_open')) {
         throw new Exception('The PHP mcrypt extension must be installed for encryption', 1);
     }
     if (!in_array(self::MCRYPT_MODULE, mcrypt_list_algorithms())) {
         throw new Exception("The cipher used self::MCRYPT_MODULE does not appear to be supported by the installed version of libmcrypt", 1);
     }
 }
Exemplo n.º 13
0
 /**
  * Validates mcrypt installation.
  *
  * @codeCoverageIgnore
  */
 private function verifyEnvironment()
 {
     if (!function_exists('mcrypt_module_open')) {
         throw new EnvironmentException('The cipher used, %1$s (also known as %2$s), requires libmcrypt version 2.4.x or newer. The version installed does not appear to meet this requirement.', 'AES-192', 'rijndael-192');
     }
     if (!in_array('rijndael-192', mcrypt_list_algorithms())) {
         throw new EnvironmentException('The cipher used, %1$s (also known as %2$s), does not appear to be supported by the installed version of libmcrypt', 'AES-192', 'rijndael-192');
     }
 }
Exemplo n.º 14
0
 /**
  * Get a list of supported ciphers for this class implementation
  *
  * @return array A list of supported ciphers
  */
 public static function getSupportedCiphers()
 {
     // @codeCoverageIgnoreStart
     if (!function_exists('mcrypt_list_algorithms')) {
         return array();
     }
     // @codeCoverageIgnoreEnd
     return mcrypt_list_algorithms();
 }
Exemplo n.º 15
0
 private function checkMcrypt($algorithm, $secret)
 {
     if (!in_array($algorithm, mcrypt_list_algorithms())) {
         throw new CryptoProviderException("Algorithm '{$algorithm}' doesn't support by mcrypt extension");
     }
     if (strlen($secret) < self::SECRET_MIN_LENGHT) {
         throw new CryptoProviderException("Secret passphrase must have more than {self::SECRET_MIN_LENGHT} characters. But '{$secret}' was given");
     }
 }
Exemplo n.º 16
0
function vidtrial_crypto_algo()
{
    $algos = mcrypt_list_algorithms();
    foreach (array("rijndael-128", "twofish", "serpent") as $algo) {
        if (in_array($algo, $algos)) {
            return $algo;
        }
    }
    throw new Exception("bad installation");
}
Exemplo n.º 17
0
 public function __construct(string $cipher = null, string $hash = null, string $mode = null, bool $twoStep = true)
 {
     parent::__construct($cipher, $hash, $mode, $twoStep);
     if (!function_exists("mcrypt_list_algorithms")) {
         throw new Exception("Could not find the MCrypt module");
     } elseif (!in_array($this->mCipher, mcrypt_list_algorithms())) {
         throw new Exception("The cipher '" . $this->mCipher . "' is not supported by this platform installation");
     } elseif (!in_array($this->mMode, mcrypt_list_modes())) {
         throw new Exception("The block mode '" . $this->mMode . "' is not supported by this platform installation");
     }
 }
 /**
  * Constructor
  */
 function dkCrypt()
 {
     if (!extension_loaded('mcrypt')) {
         exit("MCrypt libary not present. Exiting.");
         return false;
     }
     $mcryptAlgorithms = mcrypt_list_algorithms();
     if (!in_array('rijndael-256', $mcryptAlgorithms) || !in_array('tripledes', $mcryptAlgorithms)) {
         exit("MCrypt required algorithms not present. Exiting.");
         return false;
     }
 }
Exemplo n.º 19
0
 /**
  * Factory
  *
  * Create a Crypto instance using a build in encryptor with given encryptor name
  * for now support all most used MCRYPT_* algorithms
  *
  * @param string $secretKey Secret key used for encryption/decryption
  * @param string $encryptor one of MCRYPT_* constants or class or instance implementing EncryptorInterface
  *
  * @return Crypto
  * @throws AlgorithmNotSupportedException
  */
 public static function build($secretKey, $encryptor = MCRYPT_RIJNDAEL_256)
 {
     if (is_string($encryptor)) {
         $algorithms = mcrypt_list_algorithms();
         if (in_array($encryptor, $algorithms)) {
             $encryptor = new MCryptEncryptor($secretKey, $encryptor);
         } elseif (class_exists($encryptor)) {
             $encryptor = new $encryptor();
         }
     }
     return new Crypto($encryptor);
 }
Exemplo n.º 20
0
 function wrap_mcrypt_get_ciphers_params()
 {
     $ary = array();
     $algorithms = mcrypt_list_algorithms();
     foreach ($algorithms as $cipher) {
         $key_size = @mcrypt_get_key_size($cipher, 'cbc');
         $iv_size = @mcrypt_get_iv_size($cipher, 'cbc');
         if ($key_size && $key_size > 1) {
             array_push($ary, array($cipher, $key_size, $iv_size));
         }
     }
     return $ary;
 }
Exemplo n.º 21
0
Arquivo: MCrypt.php Projeto: jasny/Q
 /**
  * Decrypt encrypted value.
  *
  * @param string $value
  * @return string
  */
 public function decrypt($value)
 {
     if (empty($this->method)) {
         throw new Exception("Unable to decrypt; Algoritm not specified.");
     }
     if (!in_array($this->method, mcrypt_list_algorithms())) {
         throw new Exception("Unable to decrypt; Algoritm '{$this->method}' is not supported.");
     }
     if (class_exists('Q\\Fs_File', false) && $value instanceof Fs_File) {
         $value = $value->getContents();
     }
     return trim(mcrypt_decrypt($this->method, $this->secret, $value, $this->mode), "");
 }
Exemplo n.º 22
0
 private function getCipher()
 {
     if ($this->cipher !== false) {
         return $this->cipher;
     }
     $available = mcrypt_list_algorithms();
     foreach ($this->ciphers as $cipher) {
         if (in_array($cipher, $available)) {
             $this->cipher = $cipher;
             return constant('MCRYPT_' . Tools::strtoupper(str_replace('-', '_', $cipher)));
         }
     }
     $this->addError('no available cipher');
     return false;
 }
Exemplo n.º 23
0
 public function setCipherAlgorithm($cipherAlgorithm)
 {
     static $algos = NULL;
     if ($algos === NULL) {
         $algos = (array) mcrypt_list_algorithms();
     }
     $algo = (string) $cipherAlgorithm;
     if (!in_array($algo, $algos, true)) {
         $allow = implode(', ', array_map(function ($el) {
             return '"' . $el . '"';
         }, $algos));
         throw new \InvalidArgumentException(sprintf('Invalid cipher "%s" given, supported ciphers are %s', $algo, $allow));
     }
     $this->cipherAlgorithm = $algo;
     return $this;
 }
Exemplo n.º 24
0
 public function action_check()
 {
     $version = explode('.', phpversion());
     if ($version[0] != "5" or $version[0] == "5" and intval($version[1]) < 3) {
         return $this->_view->send_error_message("PHP 5.3 Required: Found " . implode('.', $version));
     }
     if (!function_exists('mysql_connect')) {
         return $this->_view->send_error_message("Missing MySQL support in PHP.");
     }
     if (!function_exists('imagepng')) {
         return $this->_view->send_error_message("Missing GD support in PHP.");
     }
     if (!function_exists('mcrypt_encrypt') or !in_array('rijndael-128', mcrypt_list_algorithms()) or !in_array('nofb', mcrypt_list_modes())) {
         return $this->_view->send_error_message("Missing MCrypt support or Rjindael 128.");
     }
     $this->request->redirect('/install/database');
 }
Exemplo n.º 25
0
 /**
  * Encrypts a string
  *
  * @param array $string: the string to be encrypted
  * @return array an array with the string as the first element and the initialization vector as the second element
  */
 public static function encrypt($string)
 {
     if (in_array('mcrypt', get_loaded_extensions())) {
         $encryptionAlgorithm = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['sr_freecap']['encryptionAlgorithm'];
         $availableAlgorithms = mcrypt_list_algorithms();
         if (in_array($encryptionAlgorithm, $availableAlgorithms)) {
             $key = md5($GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'], true);
             $iv = mcrypt_create_iv(mcrypt_get_iv_size($encryptionAlgorithm, MCRYPT_MODE_CBC), MCRYPT_RAND);
             $string = mcrypt_encrypt($encryptionAlgorithm, $key, $string, MCRYPT_MODE_CBC, $iv);
             $cypher = array(base64_encode($string), base64_encode($iv));
         } else {
             $cypher = array(base64_encode($string));
         }
     } else {
         $cypher = array(base64_encode($string));
     }
     return $cypher;
 }
Exemplo n.º 26
0
 /**
  * Renders a select element that allows to choose the encryption algoritm to be used by the extension
  *
  * @param array $params: Field information to be rendered
  * @param \TYPO3\CMS\Extensionmanager\ViewHelpers\Form\TypoScriptConstantsViewHelper $pObj: The calling parent object.
  * @return string The HTML select field
  */
 public function buildEncryptionAlgorithmSelector(array $params, \TYPO3\CMS\Extensionmanager\ViewHelpers\Form\TypoScriptConstantsViewHelper $pObj)
 {
     if (in_array('mcrypt', get_loaded_extensions())) {
         $encryptionAlgorithms = mcrypt_list_algorithms();
         if (!empty($encryptionAlgorithms)) {
             $field = '<br /><select id="' . $params['propertyName'] . '" name="' . $params['fieldName'] . '" />' . LF;
             foreach ($encryptionAlgorithms as $encryptionAlgorithm) {
                 $selected = $params['fieldValue'] == $encryptionAlgorithm ? 'selected="selected"' : '';
                 $field .= '<option name="' . $encryptionAlgorithm . '" value="' . $encryptionAlgorithm . '" ' . $selected . '>' . $encryptionAlgorithm . '</option>' . LF;
             }
             $field .= '</select><br /><br />' . LF;
         } else {
             $field = '<br />Available encryption algorithms could not be found. Algorithm blowfish will be used.<br />';
         }
     } else {
         $field = '<br />PHP mcrypt extension is not available.<br />';
     }
     return $field;
 }
Exemplo n.º 27
0
 private function get_algorithm()
 {
     // Initialize
     global $config;
     // Check for mcrypt extension
     if (!extension_loaded('mcrypt')) {
         return 'text';
     }
     if (!function_exists('mcrypt_encrypt')) {
         return 'text';
     }
     if (!function_exists('mcrypt_decrypt')) {
         return 'text';
     }
     if (!function_exists('mcrypt_get_iv_size')) {
         return 'text';
     }
     if (!function_exists('mcrypt_create_iv')) {
         return 'text';
     }
     // Return, if already assigned
     if ($config['mcrypt_algorithm'] != 'none') {
         return $config['mcrypt_algorithm'];
     }
     // Get supported algorithms
     $config['mcrypt_algorithm'] = 'text';
     $types = mcrypt_list_algorithms();
     $check_types = array('rijndael-256', 'rijndael-192', 'rijndael-128', 'cast-256', 'cast-128', 'blowfish', 'blowfish-compat', 'tripledes', 'des', 'twofish');
     // Check types
     foreach ($check_types as $type) {
         if (in_array($type, $types)) {
             $config['mcrypt_algorithm'] = $type;
             update_config_var('mcrypt_algorithm', $type);
             break;
         }
     }
     // Return
     return $config['mcrypt_algorithm'];
 }
 /**
  * Constructor
  *
  * @param string $secret
  * @param string $cipher
  * @param string $mode
  */
 public function __construct($secret, $cipher = 'rijndael-256', $mode = 'ctr')
 {
     if (!extension_loaded('mcrypt')) {
         throw new \RuntimeException('The mcrypt extension must be loaded.');
     }
     if (!in_array($cipher, mcrypt_list_algorithms(), true)) {
         throw new \InvalidArgumentException(sprintf('The cipher "%s" is not supported.', $cipher));
     }
     if (!in_array($mode, mcrypt_list_modes(), true)) {
         throw new \InvalidArgumentException(sprintf('The mode "%s" is not supported.', $mode));
     }
     $this->cipher = $cipher;
     $this->mode = $mode;
     if (0 === strlen($secret)) {
         throw new \InvalidArgumentException('$secret must not be empty.');
     }
     $key = hash('sha256', $secret, true);
     if (strlen($key) > ($size = mcrypt_get_key_size($this->cipher, $this->mode))) {
         $key = substr($key, 0, $size);
     }
     $this->key = $key;
 }
 /**
  * Initialize MCrypt
  *
  * @param	array	$params	Configuration parameters
  * @return	void
  */
 protected function _mcrypt_initialize($params)
 {
     if (!empty($params['cipher'])) {
         $params['cipher'] = strtolower($params['cipher']);
         $this->_cipher_alias($params['cipher']);
         if (!in_array($params['cipher'], mcrypt_list_algorithms(), TRUE)) {
             log_message('error', 'Encryption: MCrypt cipher ' . strtoupper($params['cipher']) . ' is not available.');
         } else {
             $this->_cipher = $params['cipher'];
         }
     }
     if (!empty($params['mode'])) {
         $params['mode'] = strtolower($params['mode']);
         if (!isset($this->_modes['mcrypt'][$params['mode']])) {
             log_message('error', 'Encryption: MCrypt mode ' . strtoupper($params['mode']) . ' is not available.');
         } else {
             $this->_mode = $this->_modes['mcrypt'][$params['mode']];
         }
     }
     if (isset($this->_cipher, $this->_mode)) {
         if (is_resource($this->_handle) && (strtolower(mcrypt_enc_get_algorithms_name($this->_handle)) !== $this->_cipher or strtolower(mcrypt_enc_get_modes_name($this->_handle)) !== $this->_mode)) {
             mcrypt_module_close($this->_handle);
         }
         if ($this->_handle = mcrypt_module_open($this->_cipher, '', $this->_mode, '')) {
             log_message('info', 'Encryption: MCrypt cipher ' . strtoupper($this->_cipher) . ' initialized in ' . strtoupper($this->_mode) . ' mode.');
         } else {
             log_message('error', 'Encryption: Unable to initialize MCrypt with cipher ' . strtoupper($this->_cipher) . ' in ' . strtoupper($this->_mode) . ' mode.');
         }
     }
 }
Exemplo n.º 30
0
 /**
  * Setup the fastest possible $engine
  *
  * Determines if the mcrypt (MODE_MCRYPT) $engine available
  * and usable for the current $block_size and $key_size.
  *
  * If not, the slower MODE_INTERNAL $engine will be set.
  *
  * @see setKey()
  * @see setKeyLength()
  * @see setBlockLength()
  * @access private
  */
 function _setupEngine()
 {
     if (constant('CRYPT_' . $this->const_namespace . '_MODE') == CRYPT_MODE_INTERNAL) {
         // No mcrypt support at all for rijndael
         return;
     }
     // The required mcrypt module name for the current $block_size of rijndael
     $cipher_name_mcrypt = 'rijndael-' . ($this->block_size << 3);
     // Determining the availibility/usability of $cipher_name_mcrypt
     switch (true) {
         case $this->key_size % 8:
             // mcrypt is not usable for 160/224-bit keys, only for 128/192/256-bit keys
         // mcrypt is not usable for 160/224-bit keys, only for 128/192/256-bit keys
         case !in_array($cipher_name_mcrypt, mcrypt_list_algorithms()):
             // $cipher_name_mcrypt is not available for the current $block_size
             $engine = CRYPT_MODE_INTERNAL;
             break;
         default:
             $engine = CRYPT_MODE_MCRYPT;
     }
     if ($this->engine == $engine && $this->cipher_name_mcrypt == $cipher_name_mcrypt) {
         // allready set, so we not unnecessary close $this->enmcrypt/demcrypt/ecb
         return;
     }
     // Set the $engine
     $this->engine = $engine;
     $this->cipher_name_mcrypt = $cipher_name_mcrypt;
     if ($this->enmcrypt) {
         // Closing the current mcrypt resource(s). _mcryptSetup() will, if needed,
         // (re)open them with the module named in $this->cipher_name_mcrypt
         mcrypt_module_close($this->enmcrypt);
         mcrypt_module_close($this->demcrypt);
         $this->enmcrypt = null;
         $this->demcrypt = null;
         if ($this->ecb) {
             mcrypt_module_close($this->ecb);
             $this->ecb = null;
         }
     }
 }