]> git.mxchange.org Git - core.git/commitdiff
Continued:
authorRoland Häder <roland@mxchange.org>
Sun, 17 Jan 2021 06:02:12 +0000 (07:02 +0100)
committerRoland Häder <roland@mxchange.org>
Sun, 17 Jan 2021 06:05:33 +0000 (07:05 +0100)
- "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 <roland@mxchange.org>
framework/config-global.php
framework/config/class_FrameworkConfiguration.php
framework/main/classes/index/file/stack/class_FileStackIndex.php
framework/main/classes/stacker/file/class_BaseFileStack.php
framework/main/classes/utils/crypto/class_CryptoUtils.php

index 3dc1e5e49efcc8103e5e2fae83c0169e966423ec..32bf7e07bba33bd5e9825aa2636a033d371de609 100644 (file)
@@ -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');
index 2984c376ee83d6dc7fac1397f62ca283211b15d0..e43a3b86eb773f80f96050d71df88d0d52fe6a3b 100644 (file)
@@ -41,24 +41,23 @@ use \InvalidArgumentException;
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
  */
 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]);
        }
 
        /**
index 4c91d818d899d0f2dbe74544ef385439ff89d2e0..69e97ed6df33fda4d3a6307c65f665e134465f4c 100644 (file)
@@ -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)));
        }
 
 }
index 8a81139ad29b1d15f57b6355bd710227bf0af027..725cb3577455236ce53c535c950a219487bf513b 100644 (file)
@@ -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,
index 99254d13ce7d11644585fc79c1e1c2bceb307eef..fa08a4c7598de613d5ba839407ad7b6783c1bdb6 100644 (file)
@@ -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.