3 namespace CoreFramework\Crypto\RandomNumber;
5 // Load framework stuff
6 use CoreFramework\Generic\FrameworkInterface;
9 * A standard random number generator
11 * @author Roland Haeder <webmaster@shipsimu.org>
13 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
14 * @license GNU GPL 3.0 or any newer version
15 * @link http://www.shipsimu.org
17 * This program is free software: you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation, either version 3 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with this program. If not, see <http://www.gnu.org/licenses/>.
30 class RandomNumberGenerator extends BaseFrameworkSystem {
32 * Prime number for better pseudo random numbers
37 * Add this calculated number to the rng
39 private $extraNumber = 0;
42 * Extra salt for secured hashing
44 private $extraSalt = '';
47 * Fixed salt for secured hashing
49 private $fixedSalt = '';
52 * Maximum length for random string
54 private $rndStrLen = 0;
59 private static $selfInstance = NULL;
62 * Protected constructor
64 * @param $className Name of this class
67 protected function __construct ($className = __CLASS__) {
68 // Call parent constructor
69 parent::__construct($className);
73 * Creates an instance of this class
75 * @param $extraInstance An extra instance for more salt (default: null)
76 * @return $rngInstance An instance of this random number generator
78 public static final function createRandomNumberGenerator (FrameworkInterface $extraInstance = NULL) {
79 // Is self instance set?
80 if (is_null(self::$selfInstance)) {
82 $rngInstance = new RandomNumberGenerator();
84 // Initialize the RNG now
85 $rngInstance->initRng($extraInstance);
88 self::$selfInstance = $rngInstance;
90 // Use from self instance
91 $rngInstance = self::$selfInstance;
94 // Return the instance
99 * Initializes the random number generator
101 * @param $extraInstance An extra instance for more salt (default: null)
103 * @todo Add site key for stronger salt!
105 protected function initRng ($extraInstance) {
106 // Get the prime number from config
107 $this->prime = $this->getConfigInstance()->getConfigEntry('math_prime');
109 // Calculate the extra number which is always the same unless you give
110 // a better prime number
111 $this->extraNumber = ($this->prime * $this->prime / pow(pi(), 2));
114 mt_srand((double) sqrt(microtime(TRUE) * 100000000 * $this->extraNumber));
116 // Set the server IP to cluster
117 $serverIp = 'cluster';
119 // Do we have a single server?
120 if ($this->getConfigInstance()->getConfigEntry('is_single_server') == 'Y') {
121 // Then use that IP for extra security
122 $serverIp = $this->getConfigInstance()->detectServerAddress();
125 // Yet-another fixed salt. This is not dependend on server software or date
126 if ($extraInstance instanceof FrameworkInterface) {
127 // With extra instance information
128 $this->fixedSalt = sha1(
130 $extraInstance->__toString() . ':' .
131 json_encode($this->getDatabaseInstance()->getConnectionData())
134 // Without extra information
135 $this->fixedSalt = sha1($serverIp . ':' . json_encode($this->getDatabaseInstance()->getConnectionData()));
138 // One-way data we need for "extra-salting" the random number
139 $this->extraSalt = sha1(
140 $this->fixedSalt . ':' .
141 getenv('SERVER_SOFTWARE') . ':' .
142 $this->getConfigInstance()->getConfigEntry('date_key') . ':' .
143 $this->getConfigInstance()->getConfigEntry('base_url')
146 // Get config entry for max salt length
147 $this->rndStrLen = $this->getConfigInstance()->getConfigEntry('rnd_str_length');
151 * Makes a pseudo-random string useable for salts
153 * @param $length Length of the string, default: 128
154 * @return $randomString The pseudo-random string
156 public function randomString ($length = -1) {
157 // Is the number <1, then fix it to default length
159 $length = $this->rndStrLen;
162 // Initialize the string
166 for ($idx = 0; $idx < $length; $idx++) {
167 // Add a random character and add it to our string
168 $randomString .= chr($this->randomNumber(0, 255));
171 // Return the random string a little mixed up
172 return str_shuffle($randomString);
176 * Generate a pseudo-random integer number in a given range
178 * @param $min Min value to generate
179 * @param $max Max value to generate
180 * @return $num Pseudo-random number
181 * @todo I had a better random number generator here but now it is somewhere lost :(
183 public function randomNumber ($min, $max) {
184 return mt_rand($min, $max);
188 * Getter for extra salt
192 public final function getExtraSalt () {
193 return $this->extraSalt;
197 * Getter for fixed salt
201 public final function getFixedSalt () {
202 return $this->fixedSalt;
206 * Generates a key based on if we have extra (default) or fixed salt enabled
208 * @return $key The generated key for encryption
210 public function generateKey () {
211 // Default is extra salt
212 $key = md5($this->getExtraSalt());
215 if ($this->getConfigInstance()->getConfigEntry('crypt_fixed_salt') == 'Y') {
216 $key = md5($this->getFixedSalt());