]> git.mxchange.org Git - shipsimu.git/blob - inc/classes/main/rng/class_RandomNumberGenerator.php
Salt length reduced
[shipsimu.git] / inc / classes / main / rng / class_RandomNumberGenerator.php
1 <?php
2 /**
3  * A standard random number generator
4  *
5  * @author              Roland Haeder <webmaster@ship-simu.org>
6  * @version             0.3.0
7  * @copyright   Copyright(c) 2007, 2008 Roland Haeder, this is free software
8  * @license             GNU GPL 3.0 or any newer version
9  * @link                http://www.mxchange.org
10  *
11  * This program is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23  */
24 class RandomNumberGenerator extends BaseFrameworkSystem {
25         /**
26          * Prime number for better pseudo random numbers
27          */
28         private $prime = 0;
29
30         /**
31          * Add this calculated number to the rng
32          */
33         private $extraNumber = 0;
34
35         /**
36          * Extra salt for secured hashing
37          */
38         private $extraSalt = "";
39
40         /**
41          * Maximum length for random string
42          */
43         private $rndStrLen = 0;
44
45         /**
46          * Private constructor
47          *
48          * @param       $className      Name of this class
49          * @return      void
50          */
51         protected function __construct ($className = __CLASS__) {
52                 // Call parent constructor
53                 parent::__construct($className);
54
55                 // Set part description
56                 $this->setObjectDescription("Standard random number generator");
57
58                 // Create unique ID number
59                 $this->createUniqueID();
60
61                 // Clean up a little
62                 $this->removeNumberFormaters();
63                 $this->removeSystemArray();
64         }
65
66         /**
67          * Creates an instance of this class
68          *
69          * @return      $rngInstance    An instance of this random number generator
70          */
71         public final static function createRandomNumberGenerator () {
72                 // Get a new instance
73                 $rngInstance = new RandomNumberGenerator();
74
75                 // Initialize the RNG now
76                 $rngInstance->initRng();
77
78                 // Return the instance
79                 return $rngInstance;
80         }
81
82         /**
83          * Initializes the random number generator
84          *
85          * @return      void
86          */
87         protected function initRng () {
88                 // Get the prime number from config
89                 $this->prime = $this->getConfigInstance()->readConfig('math_prime');
90
91                 // Calculate the extra number which is always the same unless you give
92                 // a better prime number
93                 $this->extraNumber = ($this->prime * $this->prime / (pi() ^ 2));
94
95                 // One-way data we need for "extra-salting" the random number
96                 // @TODO Add site for stronger salt!
97                 $this->extraSalt = sha1(getenv('SERVER_ADDR') . ":" . getenv('SERVER_SOFTWARE') . ":" . $this->getConfigInstance()->readConfig('date_key') . ":" . serialize($this->getDatabaseInstance()->getConnectionData()));
98
99                 // Get config entry for max salt length
100                 $this->rndStrLen = $this->getConfigInstance()->readConfig('rnd_str_length');
101         }
102
103         /**
104          * Makes a pseudo-random string useable for salts
105          *
106          * @param       $length                 Length of the string, default: 128
107          * @return      $randomString   The pseudo-random string
108          */
109         public function makeRandomString ($length = -1) {
110                 // Is the number <1, then fix it to default length
111                 if ($length < 1) $length = $this->rndStrLen;
112
113                 // Initialize the string
114                 $randomString = "";
115
116                 // And generate it
117                 for ($idx = 0; $idx < $length; $idx++) {
118                         // Add a random character and add it to our string
119                         $randomString .= chr($this->randomNumnber(0, 255));
120                 }
121
122                 // Return the random string mixed up
123                 return str_shuffle($randomString);
124         }
125
126         /**
127          * Generate a pseudo-random integer number in a given range
128          *
129          * @param       $min    Min value to generate
130          * @param       $max    Max value to generate
131          * @return      $num    Pseudo-random number
132          */
133         public function randomNumnber ($min, $max) {
134                 // @TODO I had a better random number generator here
135                 return mt_rand($min, $max);
136         }
137 }
138
139 // [EOF]
140 ?>