From 7e65dc99f525146ee87ba35b5329f48e07d48b12 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Sat, 25 Feb 2023 07:30:59 +0100 Subject: [PATCH] Continued: - finally removed more (by the PHP group) already removed mcrypt stuff - added more debug lines --- framework/config-global.php | 3 - .../criteria/search/class_SearchCriteria.php | 81 +++++--- .../criteria/update/class_UpdateCriteria.php | 2 + .../classes/crypto/class_CryptoHelper.php | 21 +- .../crypto/mcrypt/class_McryptStream.php | 179 ------------------ .../crypto/null/class_NullCryptoStream.php | 4 +- 6 files changed, 73 insertions(+), 217 deletions(-) delete mode 100644 framework/main/classes/streams/crypto/mcrypt/class_McryptStream.php diff --git a/framework/config-global.php b/framework/config-global.php index ffa0d9a2..ccd83da0 100644 --- a/framework/config-global.php +++ b/framework/config-global.php @@ -454,9 +454,6 @@ $cfg->setConfigEntry('thousands_separator', '.'); // CFG: DECIMALS-SEPARATOR $cfg->setConfigEntry('decimals_separator', ','); -// CFG: CRYPTO-MCRYPT-STREAM-CLASS -$cfg->setConfigEntry('crypto_mcrypt_stream_class', 'Org\Mxchange\CoreFramework\Stream\Crypto\Mcrypt\McryptStream'); - // CFG: CRYPTO-OPENSSL-STREAM-CLASS $cfg->setConfigEntry('crypto_openssl_stream_class', 'Org\Mxchange\CoreFramework\Stream\Crypto\OpenSsl\OpenSslStream'); diff --git a/framework/main/classes/criteria/search/class_SearchCriteria.php b/framework/main/classes/criteria/search/class_SearchCriteria.php index 197fc6f1..f8d4aaed 100644 --- a/framework/main/classes/criteria/search/class_SearchCriteria.php +++ b/framework/main/classes/criteria/search/class_SearchCriteria.php @@ -10,6 +10,7 @@ use Org\Mxchange\CoreFramework\Generic\FrameworkInterface; // Import SPL stuff use \InvalidArgumentException; +use \UnexpectedValueException; /** * Search criteria for e.g. searching in databases. Do not use this class if @@ -68,9 +69,11 @@ class SearchCriteria extends BaseCriteria implements LocalSearchCriteria { */ public static final function createSearchCriteria () { // Get a new instance + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('SEARCH-CRITERIA: CALLED!'); $criteriaInstance = new SearchCriteria(); // Return this instance + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('SEARCH-CRITERIA: criteriaInstance=%s - EXIT!', $criteriaInstance->__toString())); return $criteriaInstance; } @@ -88,13 +91,27 @@ class SearchCriteria extends BaseCriteria implements LocalSearchCriteria { /** * "Setter" for limit from a configuration entry * - * @param $configEntry The configuration entry which hold a number as limit + * @param $configKey The configuration entry which hold a number as limit * @return void + * @throws InvalidArgumentException If a paramter has an invalid value */ - public final function setConfiguredLimit (string $configEntry) { - // Get the limit from config entry and set it - $limit = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry($configEntry); + public final function setConfiguredLimit (string $configKey) { + // Check parameter + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('SEARCH-CRITERIA: configKey=%s - CALLED!', $configKey)); + if (empty($configKey)) { + // Throw IAE + throw new InvalidArgumentException('Parameter "configKey" is empty'); + } + + // Get the limit from config entry + $limit = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry($configKey); + + // And set it + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('SEARCH-CRITERIA: limit=%d', $limit)); $this->setLimit($limit); + + // Trace message + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('SEARCH-CRITERIA: EXIT!'); } /** @@ -135,14 +152,15 @@ class SearchCriteria extends BaseCriteria implements LocalSearchCriteria { * @param $separator Separator for "exploding" $value (default: ',') * @return $isMatching Whether the key/value is matching or excluded * @throws InvalidArgumentException If a parameter is invalid + * @throws UnexpectedValueException If $searchChoice is not an array */ public function isCriteriaMatching (string $key, $value, string $separator = ',') { // $key/$value cannot be array/NULL/bool, value can be NULL but then NULL must be loocked for - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SEARCH-CRITERIA: key=%s,value[]=%s,separator=%s - CALLED!', $key, gettype($value), $separator)); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('SEARCH-CRITERIA: key=%s,value[]=%s,separator=%s - CALLED!', $key, gettype($value), $separator)); if (empty($key)) { // Throw IAE throw new InvalidArgumentException('Parameter "key" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT); - } elseif (is_array($value) || is_bool($value) || is_bool($value) || is_object($value) || is_resource($value)) { + } elseif (is_array($value) || is_bool($value) || is_object($value) || is_resource($value)) { // Throw it again throw new InvalidArgumentException(sprintf('value[]=%s is not supported/valid', gettype($value))); } elseif (empty($separator)) { @@ -151,13 +169,16 @@ class SearchCriteria extends BaseCriteria implements LocalSearchCriteria { } // "Explode" value + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('SEARCH-CRITERIA: Invoking explode(%s,value[]=%s) ...', $seperator, gettype($value))); $valueArray = explode($separator, $value); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('SEARCH-CRITERIA: valueArray()=%d', count($valueArray))); // Get 'default' search value + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('SEARCH-CRITERIA: Invoking this->getCriteriaElemnent(%s) ...', $key)); $searchDefault = $this->getCriteriaElemnent($key); // 'default' check - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA: getCriteriaElement(' . $key . ')[' . gettype($searchDefault) . ']=' . $searchDefault); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('SEARCH-CRITERIA: searchDefault[%s]=%s', gettype($searchDefault), $searchDefault)); $isMatching = ( ( ( @@ -173,52 +194,58 @@ class SearchCriteria extends BaseCriteria implements LocalSearchCriteria { ); // Get 'choice' search value (can be NULL or $separator-separated string) - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA: getCriteriaElement(' . $key . ')[' . gettype($searchDefault) . ']=' . $searchDefault . ',isMatching=' . intval($isMatching)); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('SEARCH-CRITERIA: isMatching=%d', intval($isMatching))); $searchChoice = $this->getCriteriaChoiceElemnent($key); - // May be false or array - assert(($searchChoice === false) || (is_array($searchChoice))); + // Is an array returned? + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('SEARCH-CRITERIA: searchChoice[]=%s', gettype($searchChoice))); + if (!is_array($searchChoice)) { + // Should not happen + throw new UnexpectedValueException(sprintf('searchChoice[]=%s is unexpected', gettype($searchChoice)), FrameworkInterface::EXCEPTION_UNEXPECTED_VALUE); + } // 'choice' check - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA: getCriteriaChoiceElement(' . $key . ')[' . gettype($searchChoice) . ']=' . print_r($searchChoice, true)); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('SEARCH-CRITERIA: searchChoice(%d)=%s', count($searchChoice), print_r($searchChoice, true))); if ((is_array($searchChoice)) && (count($valueArray) == 1)) { // $value is a single-search value, so use in_[] $isMatching = ((($isMatching === true) || (($searchDefault === false) && (!is_null($value)))) && (in_array($value, $searchChoice))); // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA: getCriteriaChoiceElement(' . $key . ')[]=' . gettype($searchChoice) . ',value[' . gettype($value) . ']=' . $value . ',isMatching=' . intval($isMatching) . ' - SINGLE-MATCH'); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('SEARCH-CRITERIA: isMatching=%d - SINGLE-MATCH', intval($isMatching))); } elseif ((is_array($searchChoice)) && (count($valueArray) > 1)) { - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA: getCriteriaChoiceElement(' . $key . ')[]=' . gettype($searchChoice) . ',value[]=' . count($valueArray) . ',isMatching=' . intval($isMatching)); - // $value is choice-search value, so check all entries + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('SEARCH-CRITERIA: searchChoice()=%d,valueArray()=%d - MULTI-MATCH', count($searchChoice), count($valueArray))); $isMatching = (($isMatching === true) || (($searchDefault === false) && (!is_null($value)))); - $idx = 0; - foreach ($valueArray as $idx => $match) { - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA: match=' . $match . ',count(searchChoice)=' . count($searchChoice)); + // Loop through all values + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('SEARCH-CRITERIA: isMatching=%d - BEFORE!', intval($isMatching))); + foreach ($valueArray as $idx => $match) { // Is it found? (one is okay) + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('SEARCH-CRITERIA: idx=%d,match=%s', $idx, $match)); $isMatching = (($isMatching === true) && (in_array($match, $searchChoice))); - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA: match=' . $match . ',isMatching=' . intval($isMatching)); + // No longer matching? + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('SEARCH-CRITERIA: isMatching=%d - LOOP!', intval($isMatching))); + if (!$isMatching) { + // Skip further iterations + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage('SEARCH-CRITERIA: BREAK!'); + break; + } } // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA: getCriteriaChoiceElement(' . $key . ')[]=' . gettype($searchChoice) . ',value[]=' . count($valueArray) . ',idx=' . $idx . ',isMatching=' . intval($isMatching) . ' - CHOICE-MATCH'); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('SEARCH-CRITERIA: isMatching=%d - AFTER!', intval($isMatching))); } else { // Choice-match is false - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA: getCriteriaChoiceElement(' . $key . ')[]=' . gettype($searchChoice) . ',value[' . gettype($value) . ']=' . $value . ',isMatching=' . intval($isMatching) . ' - false-MATCH'); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('SEARCH-CRITERIA: key=%s[],searchChoice()=%d,value[%s]=%s,isMatching=%d - FALSE-MATCH', $key, count($searchChoice), gettype($value), $value, intval($isMatching))); } // Get 'exclude' search value - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA: getCriteriaChoiceElement(' . $key . ')[]=' . gettype($searchChoice) . ',isMatching=' . intval($isMatching)); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('SEARCH-CRITERIA: Invoking this->getCriteriaExcludeElemnent(%s) ...', $key)); $searchExclude = $this->getCriteriaExcludeElemnent($key); // 'exclude' check - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA: getCriteriaExcludeElement(' . $key . ')[' . gettype($searchExclude) . ']=' . $searchExclude); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('SEARCH-CRITERIA: searchExclude[%s]=%s', gettype($searchExclude), $searchExclude)); $isMatching = ( ( ( @@ -240,7 +267,7 @@ class SearchCriteria extends BaseCriteria implements LocalSearchCriteria { ); // Return result - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA: key=' . $key . ',value[' . gettype($value) . ']=' . $value . ',isMatching=' . intval($isMatching) . ' - EXIT!'); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('SEARCH-CRITERIA: isMatching=%d - EXIT!', intval($isMatching))); return $isMatching; } diff --git a/framework/main/classes/criteria/update/class_UpdateCriteria.php b/framework/main/classes/criteria/update/class_UpdateCriteria.php index 15b6cc06..238055e8 100644 --- a/framework/main/classes/criteria/update/class_UpdateCriteria.php +++ b/framework/main/classes/criteria/update/class_UpdateCriteria.php @@ -54,9 +54,11 @@ class UpdateCriteria extends BaseCriteria implements LocalUpdateCriteria { */ public static final function createUpdateCriteria () { // Get a new instance + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('UPDATE-CRITERIA: CALLED!'); $criteriaInstance = new UpdateCriteria(); // Return this instance + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('SEARCH-CRITERIA: criteriaInstance=%s - EXIT!', $criteriaInstance->__toString())); return $criteriaInstance; } diff --git a/framework/main/classes/crypto/class_CryptoHelper.php b/framework/main/classes/crypto/class_CryptoHelper.php index 8e0be3d8..e913a91b 100644 --- a/framework/main/classes/crypto/class_CryptoHelper.php +++ b/framework/main/classes/crypto/class_CryptoHelper.php @@ -73,15 +73,19 @@ class CryptoHelper extends BaseFrameworkSystem implements Cryptable { */ public static final function createCryptoHelper () { // Get a new instance + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('CRYPTO-HELPER: CALLED!'); $cryptoInstance = new CryptoHelper(); // Initialize the hasher + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('CRYPTO-HELPER: Invoking cryptoInstance->initHasher() ...'); $cryptoInstance->initHasher(); // Attach a crypto stream + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('CRYPTO-HELPER: Invoking cryptoInstance->attachCryptoStream() ...'); $cryptoInstance->attachCryptoStream(); // Return the instance + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('CRYPTO-HELPER: cryptoInstance=%s - EXIT!', $cryptoInstance->__toString())); return $cryptoInstance; } @@ -92,12 +96,14 @@ class CryptoHelper extends BaseFrameworkSystem implements Cryptable { */ public static final function getSelfInstance () { // Is no instance there? + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('CRYPTO-HELPER: self::selfInstance[]=%s - CALLED!', gettype(self::$selfInstance))); if (is_null(self::$selfInstance)) { // Then get a new one self::$selfInstance = self::createCryptoHelper(); } // Return the instance + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('CRYPTO-HELPER: self::selfInstance=%s - EXIT!', self::$selfInstance->__toString())); return self::$selfInstance; } @@ -128,17 +134,20 @@ class CryptoHelper extends BaseFrameworkSystem implements Cryptable { */ protected function attachCryptoStream () { // @TODO Maybe rewrite this with DirectoryIterator, similar to Compressor thing? - // Do we have openssl/mcrypt loaded? - if ($this->isPhpExtensionLoaded('mcrypt')) { + // Do we have openssl loaded? + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('CRYPTO-HELPER: CALLED!'); + if ($this->isPhpExtensionLoaded('openssl')) { // Then use it - $this->cryptoStreamInstance = ObjectFactory::createObjectByConfiguredName('crypto_mcrypt_stream_class', array($this->getRngInstance())); - } elseif ($this->isPhpExtensionLoaded('openssl')) { - // Then use it - $this->cryptoStreamInstance = ObjectFactory::createObjectByConfiguredName('crypto_openssl_stream_class', array($this->getRngInstance())); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage('CRYPTO-HELPER: Attaching openssl crypto stream ...'); + $this->cryptoStreamInstance = ObjectFactory::createObjectByConfiguredName('crypto_openssl_stream_class', [$this->getRngInstance()]); } else { // If nothing works ... + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage('CRYPTO-HELPER: Attaching NULL crypto stream ...'); $this->cryptoStreamInstance = ObjectFactory::createObjectByConfiguredName('crypto_null_stream_class'); } + + // Trace message + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('CRYPTO-HELPER: EXIT!'); } /** diff --git a/framework/main/classes/streams/crypto/mcrypt/class_McryptStream.php b/framework/main/classes/streams/crypto/mcrypt/class_McryptStream.php deleted file mode 100644 index bd8865f1..00000000 --- a/framework/main/classes/streams/crypto/mcrypt/class_McryptStream.php +++ /dev/null @@ -1,179 +0,0 @@ - - * @version 0.0.0 - * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2023 Core Developer Team - * @license GNU GPL 3.0 or any newer version - * @link http://www.shipsimu.org - * @todo mcrypt will become deprecated, rewrite to OpenSSL - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -class McryptStream extends BaseCryptoStream implements EncryptableStream { - /** - * Protected constructor - * - * @return void - */ - private function __construct () { - // Call parent constructor - parent::__construct(__CLASS__); - } - - /** - * Creates an instance of this node class - * - * @param $rngInstance An RNG instance - * @return $streamInstance An instance of this node class - */ - public static final function createMcryptStream (RandomNumberGenerator $rngInstance) { - // Get a new instance - $streamInstance = new McryptStream(); - - // Set the RNG instance - $streamInstance->setRngInstance($rngInstance); - - // Return the instance - return $streamInstance; - } - - /** - * Encrypt the string with fixed salt - * - * @param $str The unencrypted string - * @param $key Optional key, if none provided, a random key will be generated - * @return $encrypted Encrypted string - */ - public function encryptStream (string $str, string $key = NULL) { - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('MCRYPT-STREAM: key[' . gettype($key) . ']=' . $key); - - // Init crypto module - $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB); - $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND); - - // Generate key, if none provided - if (is_null($key)) { - // None provided - $key = $this->getRngInstance()->generateKey(); - } - - // Add some "payload" to the string - switch ($this->getRngInstance()->randomNumber(0, 8)) { - case 0: - $payloadString = crc32($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . crc32($this->getRngInstance()->randomString(20)); - break; - - case 1: - $payloadString = crc32($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . md5($this->getRngInstance()->randomString(20)); - break; - - case 2: - $payloadString = crc32($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . sha1($this->getRngInstance()->randomString(20)); - break; - - case 3: - $payloadString = md5($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . crc32($this->getRngInstance()->randomString(20)); - break; - - case 4: - $payloadString = md5($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . md5($this->getRngInstance()->randomString(20)); - break; - - case 5: - $payloadString = md5($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . sha1($this->getRngInstance()->randomString(20)); - break; - - case 6: - $payloadString = sha1($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . crc32($this->getRngInstance()->randomString(20)); - break; - - case 7: - $payloadString = sha1($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . md5($this->getRngInstance()->randomString(20)); - break; - - case 8: - $payloadString = sha1($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . sha1($this->getRngInstance()->randomString(20)); - break; - } - - // Encrypt the string - $encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $payloadString, MCRYPT_MODE_ECB, $iv); - - // Return the string - return $encrypted; - } - - /** - * Decrypt the string with fixed salt - * - * @param $encrypted Encrypted string - * @param $key Optional key, if none provided, a random key will be generated - * @return $str The unencrypted string - */ - public function decryptStream (string $encrypted, string $key = NULL) { - // Init crypto module - $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB); - $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND); - - // Shall we use a default key or custom? - if (is_null($key)) { - // Generate (default) key - $key = $this->getRngInstance()->generateKey(); - } - - // Decrypt the string - $payloadString = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $encrypted, MCRYPT_MODE_ECB, $iv); - - // Get the real string out - $strArray = explode(EncryptableStream::DATA_PAYLOAD_SEPARATOR, $payloadString); - - // Does the element count match? - assert(count($strArray) == 3); - - // Decode the string - $str = base64_decode($strArray[1]); - - // Trim trailing nulls away - $str = rtrim($str, "\0"); - - // Return the string - return $str; - } - - /** - * Streams the data and maybe does something to it - * - * @param $data The data (string mostly) to "stream" - * @return $data The data (string mostly) to "stream" - * @throws UnsupportedOperationException If this method is called (which is a mistake) - */ - public function streamData (string $data) { - self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('Unhandled ' . strlen($data) . ' bytes in this stream.'); - throw new UnsupportedOperationException([$this, __FUNCTION__], FrameworkInterface::EXCEPTION_UNSPPORTED_OPERATION); - } - -} diff --git a/framework/main/classes/streams/crypto/null/class_NullCryptoStream.php b/framework/main/classes/streams/crypto/null/class_NullCryptoStream.php index d498255f..ed7f37aa 100644 --- a/framework/main/classes/streams/crypto/null/class_NullCryptoStream.php +++ b/framework/main/classes/streams/crypto/null/class_NullCryptoStream.php @@ -9,8 +9,8 @@ use Org\Mxchange\CoreFramework\Stream\Crypto\BaseCryptoStream; use Org\Mxchange\CoreFramework\Stream\Crypto\EncryptableStream; /** - * A null-encryption stream does not encrypt anything but can be used if e.e. - * mcrypt is not installed. + * A null-encryption stream does not encrypt anything but can be used if e.g. + * openssl is not installed. * * @author Roland Haeder * @version 0.0.0 -- 2.39.5