From: Roland Häder Date: Sun, 26 Feb 2012 00:26:16 +0000 (+0000) Subject: Continued with hub development: validation of chunk hash added X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=c4cc97b7c641a57ec0630d995ca8e0d70fdeda2d;p=hub.git Continued with hub development: validation of chunk hash added --- diff --git a/application/hub/main/handler/chunks/class_ChunkHandler.php b/application/hub/main/handler/chunks/class_ChunkHandler.php index 06573bd81..14a726563 100644 --- a/application/hub/main/handler/chunks/class_ChunkHandler.php +++ b/application/hub/main/handler/chunks/class_ChunkHandler.php @@ -58,6 +58,12 @@ class ChunkHandler extends BaseHandler implements HandleableChunks, Registerable // Set the stacker in this handler $handlerInstance->setStackerInstance($stackerInstance); + // Get a crypto instance ... + $cryptoInstance = ObjectFactory::createObjectByConfiguredName('crypto_class'); + + // ... and set it in this handler + $handlerInstance->setCryptoInstance($cryptoInstance); + // Return the prepared instance return $handlerInstance; } @@ -108,7 +114,33 @@ class ChunkHandler extends BaseHandler implements HandleableChunks, Registerable assert($this->ifUnhandledChunksAvailable()); // Get an entry from the stacker - $chunk = $this->getStackerInstance()->getNamed(self::STACKER_NAME_CHUNKS_WITH_FINAL_EOP); + $chunk = $this->getStackerInstance()->popNamed(self::STACKER_NAME_CHUNKS_WITH_FINAL_EOP); + + // Split the string with proper separator character + $chunkSplits = explode(PackageFragmenter::CHUNK_DATA_HASH_SEPARATOR, $chunk); + + /* + * Make sure three elements are always found: + * 0 = Hash + * 1 = Serial number + * 2 = Raw data + */ + assert(count($chunkSplits) == 3); + + // Now hash the raw data again + $chunkHash = $this->getCryptoInstance()->hashString($chunkSplits[2], $chunkSplits[0], false); + + // Debug output + /* NOISY-DEBUG: */ $this->debugOutput('CHUNK-HANDLER: chunkHash=' . $chunkHash . ',chunkSplits[0]=' . $chunkSplits[0] . ',chunkSplits[1]=' . $chunkSplits[1]); + + // Is it the same? + if ($chunkSplits[0] != $chunkHash) { + // Re-request this chunk (trust the hash in index # 0) + $this->rerequestChunkBySplitsArray($chunkSplits); + + // Don't process this chunk + return; + } // END - if die('chunk=' . $chunk . chr(10)); } } diff --git a/application/hub/main/handler/tasks/class_TaskHandler.php b/application/hub/main/handler/tasks/class_TaskHandler.php index b9502d91e..c936419f8 100644 --- a/application/hub/main/handler/tasks/class_TaskHandler.php +++ b/application/hub/main/handler/tasks/class_TaskHandler.php @@ -213,11 +213,11 @@ class TaskHandler extends BaseHandler implements Registerable, HandleableTask { $this->getListInstance()->addEntry('tasks', $taskEntry); // Debug message - $this->debugOutput('TASK-HANDLER: Task ' . $taskName . + $this->debugOutput('TASK-HANDLER: Task registered: taskName=' . $taskName . ' (taskInstance=' . $taskInstance->__toString() . ')' . ', startupDelay=' . $taskEntry['task_startup_delay'] . 'ms' . ', intervalDelay=' . $taskEntry['task_interval_delay'] . 'ms' . - ', maxRuns=' . $taskEntry['task_max_runs'] . ' times registered.' + ', maxRuns=' . $taskEntry['task_max_runs'] . ' ...' ); } @@ -263,11 +263,8 @@ class TaskHandler extends BaseHandler implements Registerable, HandleableTask { * @return void */ public function doShutdown () { - // Should we rewind? - if (!$this->getIteratorInstance()->valid()) { - // Rewind to the beginning for next loop - $this->getIteratorInstance()->rewind(); - } // END - if + // Always rewind to the beginning for next loop + $this->getIteratorInstance()->rewind(); // Debug message $this->debugOutput('TASK-HANDLER: Shutting down all ' . $this->getListInstance()->count() . ' tasks...'); diff --git a/application/hub/main/package/fragmenter/class_PackageFragmenter.php b/application/hub/main/package/fragmenter/class_PackageFragmenter.php index ef6798126..a254b3d18 100644 --- a/application/hub/main/package/fragmenter/class_PackageFragmenter.php +++ b/application/hub/main/package/fragmenter/class_PackageFragmenter.php @@ -273,8 +273,11 @@ class PackageFragmenter extends BaseFrameworkSystem implements Fragmentable, Reg * @return $hash Hash from the raw data */ private function generateHashFromRawData ($rawData) { - // Get the crypto instance and hash the data - $hash = $this->getCryptoInstance()->hashString($rawData); + /* + * Get the crypto instance and hash the data with no extra salt because + * the other peer doesn't have *this* peer's salt. + */ + $hash = $this->getCryptoInstance()->hashString($rawData, '', false); // Return it return $hash; @@ -348,7 +351,7 @@ class PackageFragmenter extends BaseFrameworkSystem implements Fragmentable, Reg $chunk = substr($rawData, $idx, $dataChunkSize); // Hash it and remember it in seperate array - $chunkHash = $this->getCryptoInstance()->hashString($chunk); + $chunkHash = $this->getCryptoInstance()->hashString($chunk, '', false); $this->chunkHashes[$finalHash][] = $chunkHash; // Prepend the hash to the chunk @@ -396,7 +399,7 @@ class PackageFragmenter extends BaseFrameworkSystem implements Fragmentable, Reg $chunk = substr($rawData, $idx, $dataChunkSize); // Hash it and remember it in seperate array - $chunkHash = $this->getCryptoInstance()->hashString($chunk); + $chunkHash = $this->getCryptoInstance()->hashString($chunk, '', false); array_unshift($this->chunkHashes[$finalHash], $chunkHash); // Prepend the hash to the chunk