From f2b317c7477adb9912dca424bd86fa5b238d9674 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Sun, 26 Aug 2012 21:45:10 +0000 Subject: [PATCH] Rewrites - Added method createUuid() which will check if the PECL extension uuid is loaded and use that for e.g. more entropy - RandomNumberGenerator is now singelton (to save memory) - TODO added --- .../web/class_WebResendLinkCommand.php | 1 + .../main/crypto/class_CryptoHelper.php | 22 ++++++++++++++++++- inc/classes/main/menu/class_BaseMenu.php | 8 +++---- .../main/rng/class_RandomNumberGenerator.php | 22 +++++++++++++++---- 4 files changed, 44 insertions(+), 9 deletions(-) diff --git a/inc/classes/main/commands/web/class_WebResendLinkCommand.php b/inc/classes/main/commands/web/class_WebResendLinkCommand.php index 2cd6a82f..3757d4b7 100644 --- a/inc/classes/main/commands/web/class_WebResendLinkCommand.php +++ b/inc/classes/main/commands/web/class_WebResendLinkCommand.php @@ -64,6 +64,7 @@ class WebResendLinkCommand extends BaseCommand implements Commandable { $applicationInstance = $this->getResolverInstance()->getApplicationInstance(); // Get a RNG instance (Random Number Generator) + // @TODO Rewrite this code to make use of generateRandomString() from 'hub' project $rngInstance = ObjectFactory::createObjectByConfiguredName('rng_class'); // Generate a pseudo-random string diff --git a/inc/classes/main/crypto/class_CryptoHelper.php b/inc/classes/main/crypto/class_CryptoHelper.php index aa4ef887..7fc481d4 100644 --- a/inc/classes/main/crypto/class_CryptoHelper.php +++ b/inc/classes/main/crypto/class_CryptoHelper.php @@ -123,7 +123,7 @@ class CryptoHelper extends BaseFrameworkSystem implements Cryptable { */ private function generateSalt () { // Get a random string from the RNG - $randomString = $this->getRngInstance()->randomString(); + $randomString = $this->getRngInstance()->randomString() . $this->createUuid(); // Get config entry for salt length $length = $this->getConfigInstance()->getConfigEntry('salt_length'); @@ -132,6 +132,26 @@ class CryptoHelper extends BaseFrameworkSystem implements Cryptable { $this->salt = substr(sha1($randomString), -$length, $length); } + /** + * Returns a UUID (Universal Unique IDentifier) if PECL extension uuid was + * found or an empty string it not. + * + * @return $uuid UUID with leading dash or empty string + */ + public function createUuid () { + // Init empty UUID + $uuid = ''; + + // Is the UUID extension loaded? (see pecl) + if ((extension_loaded('uuid')) && (function_exists('uuid_create'))) { + // Then add it as well + $uuid = uuid_create(); + } // END - if + + // Return it + return $uuid; + } + /** * Hashes a string with salt and returns the hash. If an old previous hash * is supplied the method will use the first X chars of that hash for hashing diff --git a/inc/classes/main/menu/class_BaseMenu.php b/inc/classes/main/menu/class_BaseMenu.php index 69672112..ffebece3 100644 --- a/inc/classes/main/menu/class_BaseMenu.php +++ b/inc/classes/main/menu/class_BaseMenu.php @@ -47,23 +47,23 @@ class BaseMenu extends BaseFrameworkSystem { $this->setTemplateInstance($templateInstance); // Load the menu template for all - $this->getTemplateInstance()->loadMenuTemplate('generic_menu_entries'); + $templateInstance->loadMenuTemplate('generic_menu_entries'); // Get the 'page' from request instance $page = $this->getApplicationInstance()->getRequestInstance()->getRequestElement('page'); // Load the menu template for this page try { - $this->getTemplateInstance()->loadMenuTemplate($page . '_menu_entries'); + $templateInstance->loadMenuTemplate($page . '_menu_entries'); } catch (FileIoException $e) { // @TODO Should we log it here? We should, because it will be silently ignored. } // Get the prepared content - $menuContent = $this->getTemplateInstance()->getRawTemplateData(); + $menuContent = $templateInstance->getRawTemplateData(); // Render it here - $this->getTemplateInstance()->renderXmlContent($menuContent); + $templateInstance->renderXmlContent($menuContent); //exit(__METHOD__ . ':!OK'); } diff --git a/inc/classes/main/rng/class_RandomNumberGenerator.php b/inc/classes/main/rng/class_RandomNumberGenerator.php index bc8c42d4..6b26fe84 100644 --- a/inc/classes/main/rng/class_RandomNumberGenerator.php +++ b/inc/classes/main/rng/class_RandomNumberGenerator.php @@ -47,6 +47,11 @@ class RandomNumberGenerator extends BaseFrameworkSystem { */ private $rndStrLen = 0; + /** + * Self instance + */ + private static $selfInstance = NULL; + /** * Protected constructor * @@ -65,11 +70,20 @@ class RandomNumberGenerator extends BaseFrameworkSystem { * @return $rngInstance An instance of this random number generator */ public static final function createRandomNumberGenerator (FrameworkInterface $extraInstance = NULL) { - // Get a new instance - $rngInstance = new RandomNumberGenerator(); + // Is self instance set? + if (is_null(self::$selfInstance)) { + // Get a new instance + $rngInstance = new RandomNumberGenerator(); - // Initialize the RNG now - $rngInstance->initRng($extraInstance); + // Initialize the RNG now + $rngInstance->initRng($extraInstance); + + // Set it "self" + self::$selfInstance = $rngInstance; + } else { + // Use from self instance + $rngInstance = self::$selfInstance; + } // Return the instance return $rngInstance; -- 2.39.5