namespace Org\Mxchange\CoreFramework\Utils\Crypto;
// Import framework stuff
+use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap;
use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem;
// Import SPL stuff
*
* @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
*
*/
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 ...
*
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.
* @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')) {
+ } elseif (!extension_loaded('hash')) {
// Should be there
- throw new LogicException('Extension ext-mhash not loaded');
+ throw new LogicException('Extension ext-hash not loaded');
}
+ // 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;
}
* @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.