From 97ec849079494e71d8f759c752340a9c7fc16a88 Mon Sep 17 00:00:00 2001 From: Roland Haeder Date: Mon, 11 May 2015 21:24:07 +0200 Subject: [PATCH] Now scrypt hashes are generated for each message that is being sent. "Mining" them will later be possible. Signed-off-by: Roland Haeder --- ...class_UnexpectedPackageStatusException.php | 4 +- .../hub/main/package/class_NetworkPackage.php | 67 ++++++++----------- .../fragmenter/class_PackageFragmenter.php | 3 - core | 2 +- 4 files changed, 32 insertions(+), 44 deletions(-) diff --git a/application/hub/exceptions/package/class_UnexpectedPackageStatusException.php b/application/hub/exceptions/package/class_UnexpectedPackageStatusException.php index b07815593..3cb87887b 100644 --- a/application/hub/exceptions/package/class_UnexpectedPackageStatusException.php +++ b/application/hub/exceptions/package/class_UnexpectedPackageStatusException.php @@ -31,14 +31,14 @@ class UnexpectedPackageStatusException extends FrameworkException { */ public function __construct (array $messageArray, $code) { // Construct the message - $message = sprintf('[%s:%d] Unexpected package status %s!=%s detected, recipient=%s, sender=%s, signature=%s.', + $message = sprintf('[%s:%d] Unexpected package status %s!=%s detected, recipient=%s, sender=%s, hash=%s.', $messageArray[0]->__toString(), $this->getLine(), $messageArray[1][NetworkPackage::PACKAGE_DATA_STATUS], $messageArray[2], $messageArray[1][NetworkPackage::PACKAGE_DATA_RECIPIENT], $messageArray[1][NetworkPackage::PACKAGE_DATA_SENDER], - $messageArray[1][NetworkPackage::PACKAGE_DATA_SIGNATURE] + $messageArray[1][NetworkPackage::PACKAGE_DATA_HASH] ); // Call parent exception constructor diff --git a/application/hub/main/package/class_NetworkPackage.php b/application/hub/main/package/class_NetworkPackage.php index 72667ee5d..d0827b789 100644 --- a/application/hub/main/package/class_NetworkPackage.php +++ b/application/hub/main/package/class_NetworkPackage.php @@ -77,12 +77,12 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R const INDEX_PACKAGE_RECIPIENT = 1; const INDEX_PACKAGE_CONTENT = 2; const INDEX_PACKAGE_STATUS = 3; - const INDEX_PACKAGE_SIGNATURE = 4; + const INDEX_PACKAGE_HASH = 4; /** - * Size of the decoded data array ('status' is not included) + * Size of the decoded data array */ - const DECODED_DATA_ARRAY_SIZE = 4; + const DECODED_DATA_ARRAY_SIZE = 5; /** * Named array elements for decoded package content @@ -99,7 +99,7 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R const PACKAGE_DATA_RECIPIENT = 'recipient'; const PACKAGE_DATA_CONTENT = 'content'; const PACKAGE_DATA_STATUS = 'status'; - const PACKAGE_DATA_SIGNATURE = 'signature'; + const PACKAGE_DATA_HASH = 'hash'; /** * All package status @@ -396,8 +396,8 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R // Pop the entry (it should be it) $nextData = $this->getStackInstance()->popNamed($stackerName); - // Compare both signatures - assert($nextData[self::PACKAGE_DATA_SIGNATURE] == $packageData[self::PACKAGE_DATA_SIGNATURE]); + // Compare both hashes + assert($nextData[self::PACKAGE_DATA_HASH] == $packageData[self::PACKAGE_DATA_HASH]); // Temporary set the new status $packageData[self::PACKAGE_DATA_STATUS] = $newStatus; @@ -610,41 +610,35 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R } /** - * Generates a signature for given raw package content and sender id + * Generates a secure hash for given raw package content and sender id * * @param $content Raw package data - * @param $senderId Sender id to generate a signature for - * @return $signature Signature as BASE64-encoded string + * @param $senderId Sender id to generate a hash for + * @return $hash Hash as hex-encoded string */ - private function generatePackageSignature ($content, $senderId) { - // Hash content and sender id together, use md5() as last algo - $hash = md5($this->getCryptoInstance()->hashString($senderId . $content, $this->getPrivateKey(), FALSE)); - - // Encrypt the content again with the hash as a key - $encryptedContent = $this->getCryptoInstance()->encryptString($content, $hash); - - // Encode it with BASE64 - $signature = base64_encode($encryptedContent); + private function generatePackageHash ($content, $senderId) { + // Hash content and sender id together, use scrypt + $hash = Scrypt::hashScrypt($senderId . ':' . $content . ':' . $this->getPrivateKeyHash()); // Return it - return $signature; + return $hash; } /** - * Checks whether the signature of given package data is 'valid', here that + * Checks whether the hash of given package data is 'valid', here that * means it is the same or not. * * @param $decodedArray An array with 'decoded' (explode() was mostly called) data - * @return $isSignatureValid Whether the signature is valid - * @todo Unfinished area, signatures are currently NOT fully supported + * @return $isHashValid Whether the hash is valid + * @todo Unfinished area, hashes are currently NOT fully supported */ - private function isPackageSignatureValid (array $decodedArray) { - // Generate the signature of comparing it - $signature = $this->generatePackageSignature($decodedArray[self::INDEX_PACKAGE_CONTENT], $decodedArray[self::INDEX_PACKAGE_SENDER]); + private function isPackageHashValid (array $decodedArray) { + // Check validity + $isHashValid = Scrypt::checkScrypt($decodedArray[self::INDEX_PACKAGE_SENDER] . ':' . $decodedArray[self::INDEX_PACKAGE_CONTENT] . ':' . $this->getPrivateKeyHash(), $decodedArray[self::INDEX_PACKAGE_HASH]); - // Is it the same? - //$isSignatureValid = - exit(__METHOD__ . ': signature=' . $signature . chr(10) . ',decodedArray=' . print_r($decodedArray, TRUE)); + // Return it + //* DEBUG-DIE: */ die(__METHOD__ . ': isHashValid=' . intval($isHashValid) . chr(10) . ',decodedArray=' . print_r($decodedArray, TRUE)); + return $isHashValid; } /** @@ -693,7 +687,7 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R self::PACKAGE_DATA_RECIPIENT => $helperInstance->getRecipientType(), self::PACKAGE_DATA_CONTENT => $packageContent, self::PACKAGE_DATA_STATUS => self::PACKAGE_STATUS_NEW, - self::PACKAGE_DATA_SIGNATURE => $this->generatePackageSignature($packageContent, $this->getSessionId()) + self::PACKAGE_DATA_HASH => $this->generatePackageHash($packageContent, $this->getSessionId()) )); // Debug message @@ -1201,24 +1195,21 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R // Assert on count (should be always 3) assert(count($decodedArray) == self::DECODED_DATA_ARRAY_SIZE); - // Generate the signature of comparing it - /* - * @todo Unsupported feature of "signed" messages commented out - if (!$this->isPackageSignatureValid($decodedArray)) { + // Generate the hash of comparing it + if (!$this->isPackageHashValid($decodedArray)) { // Is not valid, so throw an exception here - exit(__METHOD__ . ':INVALID SIG! UNDER CONSTRUCTION!' . chr(10)); + exit(__METHOD__ . ':INVALID HASH! UNDER CONSTRUCTION!' . chr(10)); } // END - if - */ /* - * Create 'decodedData' array with all assoziative array elements, - * except signature. + * Create 'decodedData' array with all assoziative array elements. */ $decodedData = array( self::PACKAGE_DATA_SENDER => $decodedArray[self::INDEX_PACKAGE_SENDER], self::PACKAGE_DATA_RECIPIENT => $decodedArray[self::INDEX_PACKAGE_RECIPIENT], self::PACKAGE_DATA_CONTENT => $decodedArray[self::INDEX_PACKAGE_CONTENT], - self::PACKAGE_DATA_STATUS => self::PACKAGE_STATUS_DECODED + self::PACKAGE_DATA_STATUS => self::PACKAGE_STATUS_DECODED, + self::PACKAGE_DATA_HASH => $decodedArray[self::INDEX_PACKAGE_HASH] ); // And return it diff --git a/application/hub/main/package/fragmenter/class_PackageFragmenter.php b/application/hub/main/package/fragmenter/class_PackageFragmenter.php index 28b478b24..a816686c8 100644 --- a/application/hub/main/package/fragmenter/class_PackageFragmenter.php +++ b/application/hub/main/package/fragmenter/class_PackageFragmenter.php @@ -443,9 +443,6 @@ class PackageFragmenter extends BaseHubSystem implements Fragmentable, Registera public function fragmentPackageArray (array $packageData, ConnectionHelper $helperInstance) { // Is this package already fragmented? if (!$this->isPackageProcessed($packageData)) { - // Remove package status, the recipient doesn't need this - unset($packageData[NetworkPackage::PACKAGE_DATA_STATUS]); - // First we need to "implode" the array $rawData = implode(NetworkPackage::PACKAGE_DATA_SEPARATOR, $packageData); diff --git a/core b/core index fd0314ec6..642a5fad4 160000 --- a/core +++ b/core @@ -1 +1 @@ -Subproject commit fd0314ec6209175eff537c5358fc67484618f672 +Subproject commit 642a5fad492edc0252344a7dcaac60111367ccde -- 2.39.5