From 3edf22e2a78f8aafcdc41cff681f1c770944fe0e Mon Sep 17 00:00:00 2001
From: =?utf8?q?Roland=20H=C3=A4der?= <roland@mxchange.org>
Date: Tue, 29 Mar 2011 16:04:22 +0000
Subject: [PATCH] The previously added but not used message helper is now being
 used (and filled with 'life')

---
 application/hub/config.php                    |  3 +++
 .../class_CryptoRandomMessageHelper.php       | 27 +++++++++++++++++++
 .../class_CruncherTestUnitProducer.php        |  1 +
 .../source/units/class_TestUnitSource.php     | 27 +++++++++----------
 4 files changed, 43 insertions(+), 15 deletions(-)

diff --git a/application/hub/config.php b/application/hub/config.php
index 3377f8b2b..88fefc51f 100644
--- a/application/hub/config.php
+++ b/application/hub/config.php
@@ -573,5 +573,8 @@ $cfg->setConfigEntry('random_secret_key_length', 8);
 // CFG: TEST-UNIT-SOURCE-CLASS
 $cfg->setConfigEntry('test_unit_source_class', 'TestUnitSource');
 
+// CFG: CRYPTO-RANDOM-MESSAGE-HELPER-CLASS
+$cfg->setConfigEntry('crypto_random_message_helper_class', 'CryptoRandomMessageHelper');
+
 // [EOF]
 ?>
diff --git a/application/hub/main/helper/messages/crypto/class_CryptoRandomMessageHelper.php b/application/hub/main/helper/messages/crypto/class_CryptoRandomMessageHelper.php
index ac725f662..758006d52 100644
--- a/application/hub/main/helper/messages/crypto/class_CryptoRandomMessageHelper.php
+++ b/application/hub/main/helper/messages/crypto/class_CryptoRandomMessageHelper.php
@@ -41,9 +41,36 @@ class CryptoRandomMessageHelper extends BaseMessageHelper implements MessageHelp
 		// Get new instance
 		$messageInstance = new CryptoRandomMessageHelper();
 
+		// Create a RNG instance and set it in this class
+		$rngInstance = ObjectFactory::createObjectByConfiguredName('rng_class');
+		$messageInstance->setRngInstance($rngInstance);
+
+		// And also a crypto instance (for our encrypted messages)
+		$cryptoInstance = ObjectFactory::createObjectByConfiguredName('crypto_class');
+		$messageInstance->setCryptoInstance($cryptoInstance);
+
 		// Return the prepared instance
 		return $messageInstance;
 	}
+
+	/**
+	 * Generates an encrypted random message
+	 *
+	 * @return	$encryptedMessage	The encrypted random message
+	 */
+	public function generateRandomMessage () {
+		// Get a very secret message by encoding and random string with BASE64
+		$secretMessage = base64_encode($this->getRngInstance()->randomString($this->getConfigInstance()->getConfigEntry('random_secret_message_length')));
+
+		// Get a random, secret key
+		$secretKey = $this->getRngInstance()->randomString($this->getConfigInstance()->getConfigEntry('random_secret_key_length'));
+
+		// Now encrypt the message with our key and a good (strong) cipher
+		$encryptedMessage = base64_encode($this->getCryptoInstance()->encryptString($secretMessage, $secretKey));
+
+		// Return it
+		return $encryptedMessage;
+	}
 }
 
 // [EOF]
diff --git a/application/hub/main/producer/cruncher/work_units/class_CruncherTestUnitProducer.php b/application/hub/main/producer/cruncher/work_units/class_CruncherTestUnitProducer.php
index 02d7052ef..f735b6be6 100644
--- a/application/hub/main/producer/cruncher/work_units/class_CruncherTestUnitProducer.php
+++ b/application/hub/main/producer/cruncher/work_units/class_CruncherTestUnitProducer.php
@@ -100,6 +100,7 @@ class CruncherTestUnitProducer extends BaseUnitProducer implements UnitProducer,
 		} else {
 			// Get an encrypted, random message from our source
 			$encryptedMessage = $this->getSourceInstance()->generateMessageFromSource();
+			die($encryptedMessage."\n");
 		}
 	}
 }
diff --git a/application/hub/main/source/units/class_TestUnitSource.php b/application/hub/main/source/units/class_TestUnitSource.php
index 5a6f1886d..8aaac2f4e 100644
--- a/application/hub/main/source/units/class_TestUnitSource.php
+++ b/application/hub/main/source/units/class_TestUnitSource.php
@@ -22,6 +22,11 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 class TestUnitSource extends BaseSource implements Sourceable {
+	/**
+	 * Message helper instance
+	 */
+	private $messageInstance = null;
+
 	/**
 	 * Protected constructor
 	 *
@@ -41,14 +46,6 @@ class TestUnitSource extends BaseSource implements Sourceable {
 		// Get new instance
 		$sourceInstance = new TestUnitSource();
 
-		// Create a RNG instance and set it in this source
-		$rngInstance = ObjectFactory::createObjectByConfiguredName('rng_class');
-		$sourceInstance->setRngInstance($rngInstance);
-
-		// And also a crypto instance (for our encrypted messages)
-		$cryptoInstance = ObjectFactory::createObjectByConfiguredName('crypto_class');
-		$sourceInstance->setCryptoInstance($cryptoInstance);
-
 		// Return the prepared instance
 		return $sourceInstance;
 	}
@@ -59,14 +56,14 @@ class TestUnitSource extends BaseSource implements Sourceable {
 	 * @return	$encryptedMessage	The encrypted random message
 	 */
 	public function generateMessageFromSource () {
-		// Get a very secret message by encoding and random string with BASE64
-		$secretMessage = base64_encode($this->getRngInstance()->randomString($this->getConfigInstance()->getConfigEntry('random_secret_message_length')));
-
-		// Get a random, secret key
-		$secretKey = $this->getRngInstance()->randomString($this->getConfigInstance()->getConfigEntry('random_secret_key_length'));
+		// Is the instance set?
+		if (is_null($this->messageInstance)) {
+			// No, then create a new instance
+			$this->messageInstance = ObjectFactory::createObjectByConfiguredName('crypto_random_message_helper_class');
+		} // END - if
 
-		// Now encrypt the message with our key and a good (strong) cipher
-		$encryptedMessage = base64_encode($this->getCryptoInstance()->encryptString($secretMessage, $secretKey));
+		// Get the message from the message helper
+		$encryptedMessage = $this->messageInstance->generateRandomMessage();
 
 		// Return it
 		return $encryptedMessage;
-- 
2.39.5