<?php
-$GLOBALS['options'] = array(
+$GLOBALS['options'] = [
'analyze_input' => FALSE,
'reduce_noise' => FALSE,
'ignore_noise' => FALSE,
'keep_noise' => FALSE,
'buffer_size' => 8,
-);
+];
if (isset($_SERVER['argv'][1]) && $_SERVER['argv'][1] == 'a') {
$GLOBALS['options']['analyze_input'] = TRUE;
}
function analyzeForNoiseOnly ($data) {
- $GLOBALS['analysis']['breakdown'] = array();
+ $GLOBALS['analysis']['breakdown'] = [];
$GLOBALS['analysis']['average'] = 0;
for ($i = 0; $i < strlen($data); $i++) {
--- /dev/null
+#!/usr/bin/env php
+<?php
+error_reporting(0);
+set_time_limit(0);
+
+/*
+ * Note: I could use mhash_count() here but I like to see unavailable hashers
+ * because this is important to me to choose the most-available hasher(s) and
+ * those with the best speed/secure tradeoff.
+ */
+$hasher = hash_algos();
+
+$timers = array();
+$count = 500 * 1000;
+
+print 'Iterating ' . $count . 'x over all ' . count($hasher) . ' hash functions ...' . "\r\n";
+
+foreach ($hasher as $hash) {
+ $time = microtime(true);
+ for ($idx = 0; $idx <= $count; $idx++) {
+ $dummy = hash($hash, 'hash-test-abc-123-foo-bar');
+ }
+
+ $timers[$hash] = (microtime(true) - $time);
+ print '.';
+}
+
+print "\r\n\r\n";
+asort($timers);
+
+print 'Result from hash() benchmark (in seconds per hasher):' . "\r\n";
+print_r($timers) . "\n";
$rawData = sprintf('%s%s%s%s%s%s%s',
$stackName,
Indexable::SEPARATOR_GROUP_HASH,
- hex2bin($data[StackableFile::ARRAY_NAME_HASH]),
+ $data[StackableFile::ARRAY_NAME_HASH],
Indexable::SEPARATOR_HASH_GAP_POSITION,
$data[StackableFile::ARRAY_NAME_GAP_POSITION],
Indexable::SEPARATOR_GAP_LENGTH,
/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FILE-STACK: Calling this->readStackHeader() ...');
$this->readStackHeader();
- // Is the index loaded correctly and the stack file is just created?
+ // Is the index loaded correctly, e.g. the stack file is just created?
/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FILE-STACK: Calling this->indexInstance->isIndexLoaded() ...');
if (!$this->getIndexInstance()->isIndexLoaded()) {
/*
$rawData = sprintf('%s%s%s%s%s',
$stackName,
BinaryFile::SEPARATOR_GROUP_HASH,
- hex2bin($hash),
+ $hash,
BinaryFile::SEPARATOR_HASH_VALUE,
$encoded
);
* @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');
}
// Hash given string with (better secure) hasher
- $hash = bin2hex(mhash(MHASH_SHA256, $str));
+ $hash = hash('sha256', $str, true);
// Return it
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CRYPTO-UTILS: hash(%d){}=%s - EXIT!', strlen($hash), bin2hex($hash)));
return $hash;
}