3 namespace Org\Mxchange\CoreFramework\Crypto\RandomNumber;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap;
7 use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem;
8 use Org\Mxchange\CoreFramework\Generic\FrameworkInterface;
11 * A standard random number generator
13 * @author Roland Haeder <webmaster@shipsimu.org>
15 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2023 Core Developer Team
16 * @license GNU GPL 3.0 or any newer version
17 * @link http://www.shipsimu.org
19 * This program is free software: you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation, either version 3 of the License, or
22 * (at your option) any later version.
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
29 * You should have received a copy of the GNU General Public License
30 * along with this program. If not, see <http://www.gnu.org/licenses/>.
32 class RandomNumberGenerator extends BaseFrameworkSystem {
34 * Prime number for better pseudo random numbers
39 * Add this calculated number to the rng
41 private $extraNumber = 0;
44 * Extra salt for secured hashing
46 private $extraSalt = '';
49 * Fixed salt for secured hashing
51 private $fixedSalt = '';
54 * Maximum length for random string
56 private $rndStrLen = 0;
61 private static $selfInstance = NULL;
64 * Protected constructor
66 * @param $className Name of this class
69 private function __construct () {
70 // Call parent constructor
71 parent::__construct(__CLASS__);
75 * Creates an instance of this class
77 * @param $extraInstance An extra instance for more salt (default: null)
78 * @return $rngInstance An instance of this random number generator
80 public static final function createRandomNumberGenerator (FrameworkInterface $extraInstance = NULL) {
81 // Is self instance set?
82 if (is_null(self::$selfInstance)) {
84 $rngInstance = new RandomNumberGenerator();
86 // Initialize the RNG now
87 $rngInstance->initRng($extraInstance);
90 self::$selfInstance = $rngInstance;
92 // Use from self instance
93 $rngInstance = self::$selfInstance;
96 // Return the instance
101 * Initializes the random number generator
103 * @param $extraInstance An extra instance for more salt (default: null)
105 * @todo Add site key for stronger salt!
107 protected function initRng ($extraInstance) {
108 // Get the prime number from config
109 $this->prime = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('math_prime');
111 // Calculate the extra number which is always the same unless you give
112 // a better prime number
113 $this->extraNumber = ($this->prime * $this->prime / pow(pi(), 2));
116 mt_srand((double) sqrt(microtime(true) * 100000000 * $this->extraNumber));
118 // Set the server IP to cluster
119 $serverIp = 'cluster';
121 // Do we have a single server?
122 if (FrameworkBootstrap::getConfigurationInstance()->isEnabled('single_server')) {
123 // Then use that IP for extra security
124 $serverIp = FrameworkBootstrap::detectServerAddress();
127 // Yet-another fixed salt. This is not dependend on server software or date
128 if ($extraInstance instanceof FrameworkInterface) {
129 // With extra instance information
130 $this->fixedSalt = sha1(
132 $extraInstance->__toString() . ':' .
133 json_encode(FrameworkBootstrap::getDatabaseInstance()->getConnectionData())
136 // Without extra information
137 $this->fixedSalt = sha1($serverIp . ':' . json_encode(FrameworkBootstrap::getDatabaseInstance()->getConnectionData()));
140 // One-way data we need for "extra-salting" the random number
141 $this->extraSalt = sha1(
142 $this->fixedSalt . ':' .
143 getenv('SERVER_SOFTWARE') . ':' .
144 FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('date_key') . ':' .
145 FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('base_url')
148 // Get config entry for max salt length
149 $this->rndStrLen = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('rnd_str_length');
153 * Makes a pseudo-random string useable for salts
155 * @param $length Length of the string, default: 128
156 * @return $randomString The pseudo-random string
158 public function randomString ($length = -1) {
159 // Is the number <1, then fix it to default length
161 $length = $this->rndStrLen;
164 // Initialize the string
168 for ($idx = 0; $idx < $length; $idx++) {
169 // Add a random character and add it to our string
170 $randomString .= chr($this->randomNumber(0, 255));
173 // Return the random string a little mixed up
174 return str_shuffle($randomString);
178 * Generate a pseudo-random integer number in a given range
180 * @param $min Min value to generate
181 * @param $max Max value to generate
182 * @return $num Pseudo-random number
183 * @todo I had a better random number generator here but now it is somewhere lost :(
185 public function randomNumber ($min, $max) {
186 return mt_rand($min, $max);
190 * Getter for extra salt
194 public final function getExtraSalt () {
195 return $this->extraSalt;
199 * Getter for fixed salt
203 public final function getFixedSalt () {
204 return $this->fixedSalt;
208 * Generates a key based on if we have extra (default) or fixed salt enabled
210 * @return $key The generated key for encryption
212 public function generateKey () {
213 // Default is extra salt
214 $key = md5($this->getExtraSalt());
217 if (FrameworkBootstrap::getConfigurationInstance()->isEnabled('crypt_fixed_salt')) {
218 $key = md5($this->getFixedSalt());