]> git.mxchange.org Git - shipsimu.git/blobdiff - inc/classes/main/rng/class_RandomNumberGenerator.php
Graphical code CAPTCHA partly finished, crypto class supports encryption/decryption...
[shipsimu.git] / inc / classes / main / rng / class_RandomNumberGenerator.php
index 917739301c6fac84dc258e6aa99d2f5478a3f4b0..1e702c9769174eb55c8606479eaf462920a458a6 100644 (file)
@@ -71,14 +71,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;
@@ -87,13 +88,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');
 
@@ -101,6 +100,9 @@ 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";
 
@@ -111,7 +113,13 @@ class RandomNumberGenerator extends BaseFrameworkSystem {
                } // END - if
 
                // Yet-another fixed salt. This is not dependend on server software or date
-               $this->fixedSalt = sha1($serverIp . ":" . serialize($this->getDatabaseInstance()->getConnectionData()));
+               if ($extraInstance instanceof FrameworkInterface) {
+                       // With extra instance information
+                       $this->fixedSalt = sha1($serverIp . ":" . $extraInstance->__toString() . ":" . serialize($this->getDatabaseInstance()->getConnectionData()) . ":" . $extraInstance->getObjectDescription());
+               } else {
+                       // Without extra information
+                       $this->fixedSalt = sha1($serverIp . ":" . serialize($this->getDatabaseInstance()->getConnectionData()));
+               }
 
                // One-way data we need for "extra-salting" the random number
                $this->extraSalt = sha1($this->fixedSalt . ":" . getenv('SERVER_SOFTWARE') . ":" . $this->getConfigInstance()->readConfig('date_key'));
@@ -126,7 +134,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;
 
@@ -136,7 +144,7 @@ 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 a little mixed up
@@ -151,7 +159,7 @@ class RandomNumberGenerator extends BaseFrameworkSystem {
         * @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) {
+       public function randomNumber ($min, $max) {
                return mt_rand($min, $max);
        }