From 5ec34d0fc7387f10e400cd30b724b261a73428cc Mon Sep 17 00:00:00 2001 From: Roland Haeder Date: Tue, 12 May 2015 01:17:35 +0200 Subject: [PATCH] Rewrote determineSenderPrivateKeyHash() to accept whole raw data array. Signed-off-by: Roland Haeder --- .../hub/main/package/class_NetworkPackage.php | 25 ++++++++++++++----- 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/application/hub/main/package/class_NetworkPackage.php b/application/hub/main/package/class_NetworkPackage.php index 4fcd38b52..71e39f89b 100644 --- a/application/hub/main/package/class_NetworkPackage.php +++ b/application/hub/main/package/class_NetworkPackage.php @@ -338,15 +338,15 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R /** * Determines private key hash from given session id * - * @param $sessionId Session id - * @return $hash Private key's hash + * @param $decodedData Array with decoded data + * @return $hash Private key's hash */ - private function determineSenderPrivateKeyHash ($sessionId) { + private function determineSenderPrivateKeyHash (array $decodedData) { // Get DHT instance $dhtInstance = DhtObjectFactory::createDhtInstance('node'); // Ask DHT for session id - $senderData = $dhtInstance->findNodeLocalBySessionId($sessionId); + $senderData = $dhtInstance->findNodeLocalBySessionId($decodedData[self::]); // Is an entry found? if (count($senderData) > 0) { @@ -357,6 +357,12 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R // Return it return $senderData[NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_PRIVATE_KEY_HASH]; } // END - if + + // Make sure the requested element is there + assert(isset($decodedData[self::INDEX_PACKAGE_HASH])); + + // There is no DHT entry so, accept the hash from decoded data + return $decodedData[self::INDEX_PACKAGE_HASH]; } /** @@ -643,8 +649,15 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R * @return $hash Hash as hex-encoded string */ private function generatePackageHash ($content, $senderId) { + // Fake array + $data = array( + self::INDEX_PACKAGE_SENDER => $senderId, + self::INDEX_PACKAGE_CONTENT => $content, + self::INDEX_PACKAGE_HASH => '' + ); + // Hash content and sender id together, use scrypt - $hash = Scrypt::hashScrypt($senderId . ':' . $content . ':' . $this->determineSenderPrivateKeyHash($senderId)); + $hash = Scrypt::hashScrypt($senderId . ':' . $content . ':' . $this->determineSenderPrivateKeyHash($data)); // Return it return $hash; @@ -660,7 +673,7 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R */ private function isPackageHashValid (array $decodedArray) { // Check validity - $isHashValid = Scrypt::checkScrypt($decodedArray[self::INDEX_PACKAGE_SENDER] . ':' . $decodedArray[self::INDEX_PACKAGE_CONTENT] . ':' . $this->determineSenderPrivateKeyHash($decodedArray[self::INDEX_PACKAGE_SENDER]), $decodedArray[self::INDEX_PACKAGE_HASH]); + $isHashValid = Scrypt::checkScrypt($decodedArray[self::INDEX_PACKAGE_SENDER] . ':' . $decodedArray[self::INDEX_PACKAGE_CONTENT] . ':' . $this->determineSenderPrivateKeyHash($decodedArray); // Return it //* DEBUG-DIE: */ die(__METHOD__ . ': isHashValid=' . intval($isHashValid) . chr(10) . ',decodedArray=' . print_r($decodedArray, TRUE)); -- 2.39.5