From: Roland Häder Date: Sun, 17 Jan 2021 06:02:12 +0000 (+0100) Subject: Continued: X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=4e866984276252036eb201a322e515671472860a;p=core.git Continued: - "externalized" hard-wired hash function name "sha256" to configuration key "crypto_hash_function_name", maybe others will follow? - renamed $config array to $configData in FrameworkConfiguration class - since PHP doesn't support static initializer, e.g. like Java does, added private, static method staticInitializer() which is (in Java) executed only once when the class file has been loaded Signed-off-by: Roland Häder --- diff --git a/framework/config-global.php b/framework/config-global.php index 3dc1e5e4..32bf7e07 100644 --- a/framework/config-global.php +++ b/framework/config-global.php @@ -480,3 +480,6 @@ $cfg->setConfigEntry('crypto_null_stream_class', 'Org\Mxchange\CoreFramework\Str // CFG: DEVELOPER-MODE-ENABLED $cfg->setConfigEntry('developer_mode_enabled', true); + +// CFG: CRYPTO-HASH-FUNCTION-NAME +$cfg->setConfigEntry('crypto_hash_function_name', 'sha256'); diff --git a/framework/config/class_FrameworkConfiguration.php b/framework/config/class_FrameworkConfiguration.php index 2984c376..e43a3b86 100644 --- a/framework/config/class_FrameworkConfiguration.php +++ b/framework/config/class_FrameworkConfiguration.php @@ -41,24 +41,23 @@ use \InvalidArgumentException; * along with this program. If not, see . */ class FrameworkConfiguration implements Registerable { + // Some constants for the configuration system + const EXCEPTION_CONFIG_KEY_IS_EMPTY = 0x130; + const EXCEPTION_CONFIG_KEY_WAS_NOT_FOUND = 0x131; + const EXCEPTION_CONFIG_VALUE_TYPE_UNSUPPORTED = 0x132; /** * The framework's main configuration array which will be initialized with * hard-coded configuration data and might be overwritten/extended by * config data from the database. */ - private static $config = []; + private static $configData = []; /** * Call-back instance (unused) */ private $callbackInstance = NULL; - // Some constants for the configuration system - const EXCEPTION_CONFIG_KEY_IS_EMPTY = 0x130; - const EXCEPTION_CONFIG_KEY_WAS_NOT_FOUND = 0x131; - const EXCEPTION_CONFIG_VALUE_TYPE_UNSUPPORTED = 0x132; - /** * Default constructor, the configuration entries are static, not the * whole instance. @@ -93,7 +92,7 @@ class FrameworkConfiguration implements Registerable { } // Is it set? - $isset = ((isset(self::$config[$configKey])) || (array_key_exists($configKey, self::$config))); + $isset = ((isset(self::$configData[$configKey])) || (array_key_exists($configKey, self::$configData))); // Return the result return $isset; @@ -124,7 +123,7 @@ class FrameworkConfiguration implements Registerable { } // Return the requested value - return self::$config[$configKey]; + return self::$configData[$configKey]; } /** @@ -151,10 +150,10 @@ class FrameworkConfiguration implements Registerable { // Set the configuration value //* NOISY-DEBUG: */ print(__METHOD__ . ':configEntry=' . $configKey . ',configValue[' . gettype($configValue) . ']=' . $configValue . PHP_EOL); - self::$config[$configKey] = $configValue; + self::$configData[$configKey] = $configValue; // Resort the array - ksort(self::$config); + ksort(self::$configData); } /** @@ -164,7 +163,7 @@ class FrameworkConfiguration implements Registerable { */ public final function getConfigurationArray () { // Return it - return self::$config; + return self::$configData; } /** @@ -193,7 +192,7 @@ class FrameworkConfiguration implements Registerable { } // Unset it - unset(self::$config[$configKey]); + unset(self::$configData[$configKey]); } /** diff --git a/framework/main/classes/index/file/stack/class_FileStackIndex.php b/framework/main/classes/index/file/stack/class_FileStackIndex.php index 4c91d818..69e97ed6 100644 --- a/framework/main/classes/index/file/stack/class_FileStackIndex.php +++ b/framework/main/classes/index/file/stack/class_FileStackIndex.php @@ -105,14 +105,14 @@ class FileStackIndex extends BaseFileIndex implements IndexableStack, Registerab Indexable::SEPARATOR_GAP_LENGTH, $data[StackableFile::ARRAY_NAME_DATA_LENGTH] ); - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILE-STACK-INDEX: stackName=%s,hash=%s,rawData(%d)=%s', $stackName, $data[StackableFile::ARRAY_NAME_HASH], strlen($rawData), $rawData)); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILE-STACK-INDEX: stackName=%s,hash{}=0x%s,rawData(%d)=%s', $stackName, bin2hex($data[StackableFile::ARRAY_NAME_HASH]), strlen($rawData), $rawData)); // Search for next free gap /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILE-STACK-INDEX: Calling this->iteratorInstance->binaryFileInstance->searchNextGap(%d) ...', strlen($rawData))); $gapPosition = $this->getIteratorInstance()->getBinaryFileInstance()->searchNextGap(strlen($rawData)); // Gap position cannot be smaller or equal than header length - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILE-STACK-INDEX: stackName=%s,hash=%s,gapPosition=%s', $stackName, $data[StackableFile::ARRAY_NAME_HASH], $gapPosition)); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILE-STACK-INDEX: stackName=%s,hash{}=0x%s,gapPosition=%s', $stackName, bin2hex($data[StackableFile::ARRAY_NAME_HASH]), $gapPosition)); if ($gapPosition <= ($this->getIteratorInstance()->getBinaryFileInstance()->getHeaderSize() + 1)) { // Not valid gap position returned throw new UnexpectedValueException(sprintf('gapPosition[%s]=%d is smaller or equal headerSize+1=%d', gettype($gapPosition), $gapPosition, ($this->getIteratorInstance()->getBinaryFileInstance()->getHeaderSize() + 1))); @@ -123,7 +123,7 @@ class FileStackIndex extends BaseFileIndex implements IndexableStack, Registerab $this->getIteratorInstance()->getBinaryFileInstance()->writeData($gapPosition, $rawData); // Trace message - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILE-STACK-INDEX: stackName=%s,hash=%s,rawData()=%d - EXIT!', $stackName, $data[StackableFile::ARRAY_NAME_HASH], strlen($rawData))); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILE-STACK-INDEX: stackName=%s,hash{}=0x%s,rawData()=%d - EXIT!', $stackName, bin2hex($data[StackableFile::ARRAY_NAME_HASH]), strlen($rawData))); } } diff --git a/framework/main/classes/stacker/file/class_BaseFileStack.php b/framework/main/classes/stacker/file/class_BaseFileStack.php index 8a81139a..725cb357 100644 --- a/framework/main/classes/stacker/file/class_BaseFileStack.php +++ b/framework/main/classes/stacker/file/class_BaseFileStack.php @@ -708,7 +708,7 @@ abstract class BaseFileStack extends BaseStacker { */ public function writeDataToFreeGap (string $stackName, string $hash, string $encoded) { // Raw data been written to the file - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: stackName=%s,hash=%s,encoded()=%d - CALLED!', $stackName, $hash, strlen($encoded))); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: stackName=%s,hash{}=0x%s,encoded()=%d - CALLED!', $stackName, bin2hex($hash), strlen($encoded))); $rawData = sprintf('%s%s%s%s%s', $stackName, BinaryFile::SEPARATOR_GROUP_HASH, @@ -718,7 +718,7 @@ abstract class BaseFileStack extends BaseStacker { ); // Search for next free gap - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: stackName=%s,hash=%s,rawData()=%d', $stackName, $hash, strlen($rawData))); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: stackName=%s,hash{}=0x%s,rawData()=%d', $stackName, bin2hex($hash), strlen($rawData))); $gapPosition = $this->getIteratorInstance()->getBinaryFileInstance()->searchNextGap(strlen($rawData)); // Gap position cannot be smaller than header length + 1 @@ -733,11 +733,11 @@ abstract class BaseFileStack extends BaseStacker { } // Then write the data at that gap - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: stackName=%s,hash=%s,gapPosition=%s', $stackName, $hash, $gapPosition)); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: stackName=%s,hash{}=0x%s,gapPosition=%s', $stackName, bin2hex($hash), $gapPosition)); $this->getIteratorInstance()->getBinaryFileInstance()->writeData($gapPosition, $rawData); // Return gap position, hash and length of raw data - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: stackName=%s,hash=%s,rawData()=%d - EXIT!', $stackName, $hash, strlen($rawData))); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: stackName=%s,hash{}=0x%s,rawData()=%d - EXIT!', $stackName, bin2hex($hash), strlen($rawData))); return [ StackableFile::ARRAY_NAME_GAP_POSITION => $gapPosition, StackableFile::ARRAY_NAME_HASH => $hash, diff --git a/framework/main/classes/utils/crypto/class_CryptoUtils.php b/framework/main/classes/utils/crypto/class_CryptoUtils.php index 99254d13..fa08a4c7 100644 --- a/framework/main/classes/utils/crypto/class_CryptoUtils.php +++ b/framework/main/classes/utils/crypto/class_CryptoUtils.php @@ -3,6 +3,7 @@ namespace Org\Mxchange\CoreFramework\Utils\Crypto; // Import framework stuff +use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap; use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem; // Import SPL stuff @@ -37,6 +38,11 @@ final class CryptoUtils extends BaseFrameworkSystem { */ private static $hashLength = NULL; + /** + * Hash function, will be overwritten, so don't set it here! + */ + private static $hashFunction = ''; + /** * Private constructor, no instance needed. If PHP would have a static initializer ... * @@ -47,6 +53,21 @@ final class CryptoUtils extends BaseFrameworkSystem { parent::__construct(__CLASS__); } + /** + * Since PHP doesn't have static initializers, this method needs to be + * invoked by each public method here. + */ + private static function staticInitializer () { + // Is $hashFunction set? + if (empty(self::$hashFunction)) { + // Get instance + $dummyInstance = new CryptoUtils(); + + // Set hash function from configuration + self::$hashFunction = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('crypto_hash_function_name'); + } + } + /** * Hashes a given string with a simple but stronger hash function (no salt) * and hex-encode it. @@ -58,7 +79,7 @@ final class CryptoUtils extends BaseFrameworkSystem { */ public static final function hash (string $str) { // Validate parameter/mhash extension - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CRYPTO-UTILS: str=%s - CALLED!', $str)); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CRYPTO-UTILS: str=%s - CALLED!', $str)); if (empty($str)) { // Throw IAE throw new InvalidArgumentException('Parameter "str" is empty'); @@ -67,11 +88,14 @@ final class CryptoUtils extends BaseFrameworkSystem { throw new LogicException('Extension ext-hash not loaded'); } + // Invoke static initializer + self::staticInitializer(); + // Hash given string with (better secure) hasher - $hash = hash('sha256', $str, true); + $hash = hash(self::$hashFunction, $str, true); // Return it - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CRYPTO-UTILS: hash(%d){}=%s - EXIT!', strlen($hash), bin2hex($hash))); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CRYPTO-UTILS: hash{}=0x%s - EXIT!', bin2hex($hash))); return $hash; } @@ -82,6 +106,9 @@ final class CryptoUtils extends BaseFrameworkSystem { * @return $length Length of hash() output */ public static final function getHashLength () { + // Invoke static initializer + self::staticInitializer(); + // Is it cashed? if (is_null(self::$hashLength)) { // No, then hash a string and save its length.