X-Git-Url: https://git.mxchange.org/?p=mailer.git;a=blobdiff_plain;f=inc%2Fclasses%2Fmain%2Frng%2Fclass_RandomNumberGenerator.php;h=93ba3988ed0ae28fa27f7aca7beec7b64a592724;hp=b50af11d865a3f3b051be397b5ff13b96d441ecd;hb=7940f3ad709afe4ba58012aa835c82cecc9328cc;hpb=42bc0e1fc5ae4653fe04c9d41474c874a0050b69 diff --git a/inc/classes/main/rng/class_RandomNumberGenerator.php b/inc/classes/main/rng/class_RandomNumberGenerator.php index b50af11d86..93ba3988ed 100644 --- a/inc/classes/main/rng/class_RandomNumberGenerator.php +++ b/inc/classes/main/rng/class_RandomNumberGenerator.php @@ -3,10 +3,10 @@ * A standard random number generator * * @author Roland Haeder - * @version 0.3.0 - * @copyright Copyright(c) 2007, 2008 Roland Haeder, this is free software + * @version 0.0.0 + * @copyright Copyright (c) 2007, 2008 Roland Haeder, this is free software * @license GNU GPL 3.0 or any newer version - * @link http://www.mxchange.org + * @link http://www.ship-simu.org * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -19,7 +19,7 @@ * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License - * along with this program. If not, see . + * along with this program. If not, see . */ class RandomNumberGenerator extends BaseFrameworkSystem { /** @@ -37,6 +37,11 @@ class RandomNumberGenerator extends BaseFrameworkSystem { */ private $extraSalt = ""; + /** + * Fixed salt for secured hashing + */ + private $fixedSalt = ""; + /** * Maximum length for random string */ @@ -52,12 +57,6 @@ class RandomNumberGenerator extends BaseFrameworkSystem { // Call parent constructor parent::__construct($className); - // Set part description - $this->setObjectDescription("Standard random number generator"); - - // Create unique ID number - $this->generateUniqueId(); - // Clean up a little $this->removeNumberFormaters(); $this->removeSystemArray(); @@ -66,14 +65,15 @@ class RandomNumberGenerator extends BaseFrameworkSystem { /** * Creates an instance of this class * + * @param $extraInstance An extra instance for more salt (default: null) * @return $rngInstance An instance of this random number generator */ - public final static function createRandomNumberGenerator () { + public final static function createRandomNumberGenerator (FrameworkInterface $extraInstance = null) { // Get a new instance $rngInstance = new RandomNumberGenerator(); // Initialize the RNG now - $rngInstance->initRng(); + $rngInstance->initRng($extraInstance); // Return the instance return $rngInstance; @@ -82,12 +82,11 @@ class RandomNumberGenerator extends BaseFrameworkSystem { /** * Initializes the random number generator * + * @param $extraInstance An extra instance for more salt (default: null) * @return void + * @todo Add site key for stronger salt! */ - protected function initRng () { - // Seed mt_rand() - mt_srand((double) microtime() * 1000000); - + protected function initRng ($extraInstance) { // Get the prime number from config $this->prime = $this->getConfigInstance()->readConfig('math_prime'); @@ -95,17 +94,29 @@ class RandomNumberGenerator extends BaseFrameworkSystem { // a better prime number $this->extraNumber = ($this->prime * $this->prime / (pi() ^ 2)); + // Seed mt_rand() + mt_srand((double) sqrt(microtime() * 100000000 * $this->extraNumber)); + // Set the server IP to cluster $serverIp = "cluster"; + // Do we have a single server? - if ($this->getConfigInstance()->readConfig('is_single_server') == "Y") { + if ($this->getConfigInstance()->readConfig('is_single_server') === "Y") { // Then use that IP for extra security $serverIp = getenv('SERVER_ADDR'); + } // END - if + + // Yet-another fixed salt. This is not dependend on server software or date + if ($extraInstance instanceof FrameworkInterface) { + // With extra instance information + $this->fixedSalt = sha1($serverIp . ":" . $extraInstance->__toString() . ":" . serialize($this->getDatabaseInstance()->getConnectionData())); + } else { + // Without extra information + $this->fixedSalt = sha1($serverIp . ":" . serialize($this->getDatabaseInstance()->getConnectionData())); } // One-way data we need for "extra-salting" the random number - // @TODO Add site for stronger salt! - $this->extraSalt = sha1($serverIp . ":" . getenv('SERVER_SOFTWARE') . ":" . $this->getConfigInstance()->readConfig('date_key') . ":" . serialize($this->getDatabaseInstance()->getConnectionData())); + $this->extraSalt = sha1($this->fixedSalt . ":" . getenv('SERVER_SOFTWARE') . ":" . $this->getConfigInstance()->readConfig('date_key') . $this->getConfigInstance()->readConfig('base_url')); // Get config entry for max salt length $this->rndStrLen = $this->getConfigInstance()->readConfig('rnd_str_length'); @@ -117,7 +128,7 @@ class RandomNumberGenerator extends BaseFrameworkSystem { * @param $length Length of the string, default: 128 * @return $randomString The pseudo-random string */ - public function makeRandomString ($length = -1) { + public function randomString ($length = -1) { // Is the number <1, then fix it to default length if ($length < 1) $length = $this->rndStrLen; @@ -127,10 +138,10 @@ class RandomNumberGenerator extends BaseFrameworkSystem { // And generate it for ($idx = 0; $idx < $length; $idx++) { // Add a random character and add it to our string - $randomString .= chr($this->randomNumnber(0, 255)); + $randomString .= chr($this->randomNumber(0, 255)); } - // Return the random string mixed up + // Return the random string a little mixed up return str_shuffle($randomString); } @@ -140,9 +151,9 @@ class RandomNumberGenerator extends BaseFrameworkSystem { * @param $min Min value to generate * @param $max Max value to generate * @return $num Pseudo-random number + * @todo I had a better random number generator here but now it is somewhere lost :( */ - public function randomNumnber ($min, $max) { - // @TODO I had a better random number generator here + public function randomNumber ($min, $max) { return mt_rand($min, $max); } @@ -154,6 +165,15 @@ class RandomNumberGenerator extends BaseFrameworkSystem { public final function getExtraSalt () { return $this->extraSalt; } + + /** + * Getter for fixed salt + * + * @return $fixedSalt + */ + public final function getFixedSalt () { + return $this->fixedSalt; + } } // [EOF]