]> git.mxchange.org Git - hub.git/commitdiff
Rewrote determineSenderPrivateKeyHash() to accept whole raw data array.
authorRoland Haeder <roland@mxchange.org>
Mon, 11 May 2015 23:17:35 +0000 (01:17 +0200)
committerRoland Haeder <roland@mxchange.org>
Mon, 11 May 2015 23:17:35 +0000 (01:17 +0200)
Signed-off-by: Roland Haeder <roland@mxchange.org>
application/hub/main/package/class_NetworkPackage.php

index 4fcd38b52df7e442549a6ae122c17c868cd0ac28..71e39f89b4b2dbc6477d5c8baa6ea15a5160996f 100644 (file)
@@ -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));