// 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]
* 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
*
// 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;
}
* @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;