* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
abstract class BaseFrameworkSystem extends stdClass implements FrameworkInterface {
- /**
- * Length of output from hash()
- */
- private static $hashLength = NULL;
-
/**
* Self-referencing instance
*/
return $executionTime;
}
- /**
- * 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
- */
- public static final function hash (string $str) {
- // Hash given string with (better secure) hasher
- $hash = bin2hex(mhash(MHASH_SHA256, $str));
-
- // Return it
- return $hash;
- }
-
- /**
- * "Getter" for length of hash() output. This will be "cached" to speed up
- * things.
- *
- * @return $length Length of hash() output
- */
- public static final function getHashLength () {
- // Is it cashed?
- if (is_null(self::$hashLength)) {
- // No, then hash a string and save its length.
- self::$hashLength = strlen(self::hash('abc123'));
- }
-
- // Return it
- return self::$hashLength;
- }
-
/**
* Determines if an element is set in the generic array
*
use Org\Mxchange\CoreFramework\Filesystem\Index\IndexableFile;
use Org\Mxchange\CoreFramework\Generic\UnsupportedOperationException;
use Org\Mxchange\CoreFramework\Index\Indexable;
+use Org\Mxchange\CoreFramework\Utils\Crypto\CryptoUtils;
// Import SPL stuff
use \BadMethodCallException;
* @param $value Value to be added to the stack
* @return $data Hash and gap position
* @throws InvalidArgumentException If a parameter is not valid
+ * @throws BadMethodCallException If this->indexInstance is not properly set
*/
public function writeValueToFile (string $stackName, $value) {
// Validate parameter
} elseif (is_object($value) || is_resource($value)) {
// Not wanted here
throw new InvalidArgumentException(sprintf('value[]=%s is not stackable in files', gettype($value)));
+ } elseif (!($this->getIndexInstance() instanceof Indexable)) {
+ // Index instance not set
+ throw new BadMethodCallException('this->indexInstance[] is not properly set.');
}
// Encode/convert the value into a "binary format"
// Get a strong hash for the "encoded" data
/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('INDEX-FILE: encoded=%s', $encoded));
- $hash = self::hash($encoded);
+ $hash = CryptoUtils::hash($encoded);
// Then write it to the next free gap
/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('INDEX-FILE: hash=%s', $hash));
use Org\Mxchange\CoreFramework\Filesystem\File\BaseBinaryFile;
use Org\Mxchange\CoreFramework\Generic\UnsupportedOperationException;
use Org\Mxchange\CoreFramework\Stack\File\StackableFile;
+use Org\Mxchange\CoreFramework\Utils\Crypto\CryptoUtils;
use Org\Mxchange\CoreFramework\Utils\Strings\StringUtils;
// Import SPL stuff
$encoded = StringUtils::encodeData($value);
// Get a strong hash for the "encoded" data
- $hash = self::hash($encoded);
+ $hash = CryptoUtils::hash($encoded);
// Then write it to the next free gap
$data = $this->getStackInstance()->writeDataToFreeGap($stackName, $hash, $encoded);
$data = substr($data, 0, -1);
// And update seek position
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FILE-INDEX: Calling this->iteratorInstance->updateSeekPosition() ...');
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FILE-INDEX: Calling this->iteratorInstance->binaryFileInstance->updateSeekPosition() ...');
$this->getIteratorInstance()->getBinaryFileInstance()->updateSeekPosition();
/*
);
// Write it to disk (header is always at seek position 0)
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-INDEX: Calling this->iteratorInstance->writeAtPosition(0, header=%s) ...', $header));
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-INDEX: Calling this->iteratorInstance->binaryFileInstance->writeAtPosition(0, header=%s) ...', $header));
$this->getIteratorInstance()->getBinaryFileInstance()->writeAtPosition(0, $header);
// Trace message
$this->getIteratorInstance()->getBinaryFileInstance()->setHeaderSize($headerSize);
// Init counters and gaps array
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FILE-INDEX: Calling this->iteratorInstance->initCountersGapsArray() ...');
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FILE-INDEX: Calling this->iteratorInstance->binaryFileInstance->initCountersGapsArray() ...');
$this->getIteratorInstance()->getBinaryFileInstance()->initCountersGapsArray();
// Default is not created
// Is the file's header initialized?
if (!$this->getIteratorInstance()->getBinaryFileInstance()->isFileHeaderInitialized()) {
// First pre-allocate a bit
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FILE-INDEX: Calling this->iteratorInstance->preAllocateFile(index) ...');
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FILE-INDEX: Calling this->iteratorInstance->binaryFileInstance->preAllocateFile(index) ...');
$this->getIteratorInstance()->getBinaryFileInstance()->preAllocateFile('index');
// Then write file header
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FILE-INDEX: Calling this->iteratorInstance->createFileHeader() ...');
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FILE-INDEX: Calling this->iteratorInstance->binaryFileInstance->createFileHeader() ...');
$this->getIteratorInstance()->getBinaryFileInstance()->createFileHeader();
// Mark as freshly created
/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-INDEX: created=%d', intval($created)));
if (!$created) {
// Analyze file structure
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FILE-INDEX: Calling this->iteratorInstance->analyzeFileStructure() ...');
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FILE-INDEX: Calling this->iteratorInstance->binaryFileInstance->analyzeFileStructure() ...');
$this->getIteratorInstance()->getBinaryFileInstance()->analyzeFileStructure();
}
}
// Call iterated object's method
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-INDEX: Calling this->iteratorInstance->writeAtPosition(%d, %s) ...', $seekPosition, $dataStream));
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-INDEX: Calling this->iteratorInstance->binaryFileInstance->writeAtPosition(%d, %s) ...', $seekPosition, $dataStream));
$status = $this->getIteratorInstance()->getBinaryFileInstance()->writeAtPosition($seekPosition, $dataStream);
// Return status
throw new InvalidArgumentException('Parameter "data" is an empty array');
} elseif (!isset($data[StackableFile::ARRAY_NAME_HASH])) {
// Important array element missing
- throw new InvalidArgumentException(sprintf('data[%s] not found', $data[StackableFile::ARRAY_NAME_HASH]));
+ throw new InvalidArgumentException(sprintf('data[%s] not found', StackableFile::ARRAY_NAME_HASH));
} elseif (!isset($data[StackableFile::ARRAY_NAME_GAP_POSITION])) {
// Important array element missing
- throw new InvalidArgumentException(sprintf('data[%s] not found', $data[StackableFile::ARRAY_NAME_GAP_POSITION]));
+ throw new InvalidArgumentException(sprintf('data[%s] not found', StackableFile::ARRAY_NAME_GAP_POSITION));
} elseif (!isset($data[StackableFile::ARRAY_NAME_DATA_LENGTH])) {
// Important array element missing
- throw new InvalidArgumentException(sprintf('data[%s] not found', $data[StackableFile::ARRAY_NAME_DATA_LENGTH]));
+ throw new InvalidArgumentException(sprintf('data[%s] not found', StackableFile::ARRAY_NAME_DATA_LENGTH));
}
// Raw data been written to the file
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));
// Search for next free gap
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILE-STACK-INDEX: stackName=%s,hash=%s,rawData()=%d', $stackName, $data[StackableFile::ARRAY_NAME_HASH], strlen($rawData)));
- $gapPosition = $this->getIteratorInstance()->searchNextGap(strlen($rawData));
+ /* 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));
}
// Then write the data at that gap
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILE-STACK-INDEX: Calling this->iteratorInstance->writeData(%d,%s) ...', $gapPosition, $rawData));
- $this->getIteratorInstance()->writeData($gapPosition, $rawData);
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILE-STACK-INDEX: Calling this->iteratorInstance->binaryFileInstance->writeData(%d,%s) ...', $gapPosition, $rawData));
+ $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)));
]);
// Check if the array has only 3 elements
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: HEADER_STACK_ELEMENT_COUNT=%d,header(%d)=%s', BinaryFile::HEADER_STACK_ELEMENT_COUNT, count($header), print_r($header, true)));
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: HEADER_STACK_ELEMENT_COUNT=%d,header()=%d', BinaryFile::HEADER_STACK_ELEMENT_COUNT, count($header)));
+ //* PRINTR-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: HEADER_STACK_ELEMENT_COUNT=%d,header(%d)=%s', BinaryFile::HEADER_STACK_ELEMENT_COUNT, count($header), print_r($header, true)));
if (count($header) != BinaryFile::HEADER_STACK_ELEMENT_COUNT) {
// Header array count is not expected
throw new UnexpectedValueException(sprintf('data=%s has %d elements, but expected is %d',
--- /dev/null
+<?php
+// Own namespace
+namespace Org\Mxchange\CoreFramework\Utils\Crypto;
+
+// Import framework stuff
+use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem;
+
+// Import SPL stuff
+use \InvalidArgumentException;
+use \LogicException;
+
+/**
+ * Crypto utilities class
+ *
+ * @author Roland Haeder <webmaster@shipsimu.org>
+ * @version 0.0.0
+ * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2021 Core Developer Team
+ * @license GNU GPL 3.0 or any newer version
+ * @link http://www.shipsimu.org
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+final class CryptoUtils extends BaseFrameworkSystem {
+ /**
+ * Length of output from hash()
+ */
+ private static $hashLength = NULL;
+
+ /**
+ * Private constructor, no instance needed. If PHP would have a static initializer ...
+ *
+ * @return void
+ */
+ private function __construct () {
+ // Call parent constructor
+ parent::__construct(__CLASS__);
+ }
+
+ /**
+ * 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
+ */
+ public static final function hash (string $str) {
+ // Validate parameter/mhash extension
+ if (empty($str)) {
+ // Throw IAE
+ throw new InvalidArgumentException('Parameter "str" is empty');
+ } elseif (!extension_loaded('mhash')) {
+ // Should be there
+ throw new LogicException('Extension ext-mhash not loaded');
+ }
+
+ // Hash given string with (better secure) hasher
+ $hash = bin2hex(mhash(MHASH_SHA256, $str));
+
+ // Return it
+ return $hash;
+ }
+
+ /**
+ * "Getter" for length of hash() output. This will be "cached" to speed up
+ * things.
+ *
+ * @return $length Length of hash() output
+ */
+ public static final function getHashLength () {
+ // Is it cashed?
+ if (is_null(self::$hashLength)) {
+ // No, then hash a string and save its length.
+ self::$hashLength = strlen(self::hash('abc123'));
+ }
+
+ // Return it
+ return self::$hashLength;
+ }
+
+}