]> 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 3677b4e40993faedfd22cc254b05bec1250b34d8..6d8ac999019bb2f32f958d8fe5086f360b244b37 100644 (file)
@@ -3,6 +3,8 @@
 namespace Org\Mxchange\CoreFramework\Utils\Crypto;
 
 // Import framework stuff
+use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap;
+use Org\Mxchange\CoreFramework\Generic\FrameworkInterface;
 use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem;
 
 // Import SPL stuff
@@ -14,7 +16,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 +39,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 +54,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.
@@ -54,22 +76,27 @@ final class CryptoUtils extends BaseFrameworkSystem {
         * @param       $str    The string to be hashed
         * @return      $hash   The hash from string $str
         * @throws      InvalidArgumentException        If a parameter is not valid
-        * @throws      LogicException  If proper extension ext-mhash is not loaded
+        * @throws      LogicException  If proper extension hash is not loaded
         */
        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));
                if (empty($str)) {
                        // Throw IAE
-                       throw new InvalidArgumentException('Parameter "str" is empty');
-               } elseif (!extension_loaded('mhash')) {
+                       throw new InvalidArgumentException('Parameter "str" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
+               } elseif (!extension_loaded('hash')) {
                        // Should be there
-                       throw new LogicException('Extension ext-mhash not loaded');
+                       throw new LogicException('Extension ext-hash not loaded', FrameworkInterface::EXCEPTION_LOGIC_EXCEPTION);
                }
 
+               // Invoke static initializer
+               self::staticInitializer();
+
                // Hash given string with (better secure) hasher
-               $hash = bin2hex(mhash(MHASH_SHA256, $str));
+               $hash = hash(self::$hashFunction, $str, true);
 
                // Return it
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CRYPTO-UTILS: hash{}=0x%s - EXIT!', bin2hex($hash)));
                return $hash;
        }
 
@@ -80,6 +107,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.