// CFG: DEVELOPER-MODE-ENABLED
$cfg->setConfigEntry('developer_mode_enabled', true);
+
+// CFG: CRYPTO-HASH-FUNCTION-NAME
+$cfg->setConfigEntry('crypto_hash_function_name', 'sha256');
* 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.
}
// 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;
}
// Return the requested value
- return self::$config[$configKey];
+ return self::$configData[$configKey];
}
/**
// 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);
}
/**
*/
public final function getConfigurationArray () {
// Return it
- return self::$config;
+ return self::$configData;
}
/**
}
// Unset it
- unset(self::$config[$configKey]);
+ unset(self::$configData[$configKey]);
}
/**
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)));
$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)));
}
}
*/
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,
);
// 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
}
// 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,
namespace Org\Mxchange\CoreFramework\Utils\Crypto;
// Import framework stuff
+use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap;
use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem;
// Import SPL stuff
*/
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.
*/
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');
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;
}
* @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.