]> git.mxchange.org Git - shipsimu.git/blobdiff - inc/classes/main/rng/class_RandomNumberGenerator.php
Crypto helper and RNG added (weak!)
[shipsimu.git] / inc / classes / main / rng / class_RandomNumberGenerator.php
diff --git a/inc/classes/main/rng/class_RandomNumberGenerator.php b/inc/classes/main/rng/class_RandomNumberGenerator.php
new file mode 100644 (file)
index 0000000..5856b9e
--- /dev/null
@@ -0,0 +1,140 @@
+<?php
+/**
+ * A standard random number generator
+ *
+ * @author             Roland Haeder <webmaster@ship-simu.org>
+ * @version            0.3.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
+ *
+ * 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
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+class RandomNumberGenerator extends BaseFrameworkSystem {
+       /**
+        * Prime number for better pseudo random numbers
+        */
+       private $prime = 0;
+
+       /**
+        * Add this calculated number to the rng
+        */
+       private $extraNumber = 0;
+
+       /**
+        * Extra salt for secured hashing
+        */
+       private $extraSalt = "";
+
+       /**
+        * Maximum length for salt
+        */
+       private $saltLength = 0;
+
+       /**
+        * Private constructor
+        *
+        * @param       $className      Name of this class
+        * @return      void
+        */
+       protected function __construct ($className = __CLASS__) {
+               // Call parent constructor
+               parent::__construct($className);
+
+               // Set part description
+               $this->setObjectDescription("Standard random number generator");
+
+               // Create unique ID number
+               $this->createUniqueID();
+
+               // Clean up a little
+               $this->removeNumberFormaters();
+               $this->removeSystemArray();
+       }
+
+       /**
+        * Creates an instance of this class
+        *
+        * @return      $rngInstance    An instance of this random number generator
+        */
+       public final static function createRandomNumberGenerator () {
+               // Get a new instance
+               $rngInstance = new RandomNumberGenerator();
+
+               // Initialize the RNG now
+               $rngInstance->initRng();
+
+               // Return the instance
+               return $rngInstance;
+       }
+
+       /**
+        * Initializes the random number generator
+        *
+        * @return      void
+        */
+       protected function initRng () {
+               // Get the prime number from config
+               $this->prime = $this->getConfigInstance()->readConfig('math_prime');
+
+               // Calculate the extra number which is always the same unless you give
+               // a better prime number
+               $this->extraNumber = ($this->prime * $this->prime / (pi() ^ 2));
+
+               // One-way data we need for "extra-salting" the random number
+               // @TODO Add site for stronger salt!
+               $this->extraSalt = sha1(getenv('SERVER_ADDR') . ":" . getenv('SERVER_SOFTWARE') . ":" . $this->getConfigInstance()->readConfig('date_key') . ":" . serialize($this->getDatabaseInstance()->getConnectionData()));
+
+               // Get config entry for max salt length
+               $this->saltLength = $this->getConfigInstance()->readConfig('salt_length');
+       }
+
+       /**
+        * Makes a pseudo-random string useable for salts
+        *
+        * @param       $length                 Length of the string, default: 128
+        * @return      $randomString   The pseudo-random string
+        */
+       public function makeRandomString ($length = -1) {
+               // Is the number <1, then fix it to default length
+               if ($length < 1) $length = $this->saltLength;
+
+               // Initialize the string
+               $randomString = "";
+
+               // 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));
+               }
+
+               // Return the random string mixed up
+               return str_shuffle($randomString);
+       }
+
+       /**
+        * Generate a pseudo-random integer number in a given range
+        *
+        * @param       $min    Min value to generate
+        * @param       $max    Max value to generate
+        * @return      $num    Pseudo-random number
+        */
+       public function randomNumnber ($min, $max) {
+               // @TODO I had a better random number generator here
+               return mt_rand($min, $max);
+       }
+}
+
+// [EOF]
+?>