]> git.mxchange.org Git - core.git/blobdiff - framework/main/classes/utils/crypto/class_CryptoUtils.php
Continued:
[core.git] / framework / main / classes / utils / crypto / class_CryptoUtils.php
index 99254d13ce7d11644585fc79c1e1c2bceb307eef..5db28e4a3e005ee030d032034faaef4d43afa5bc 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
@@ -14,7 +15,7 @@ use \LogicException;
  *
  * @author             Roland Haeder <webmaster@shipsimu.org>
  * @version            0.0.0
- * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2021 Core Developer Team
+ * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2022 Core Developer Team
  * @license            GNU GPL 3.0 or any newer version
  * @link               http://www.shipsimu.org
  *
@@ -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.