Ivan - Fix linear congruential generator

This commit is contained in:
ivan 2017-10-31 18:31:14 -03:00
parent 84b128326c
commit aa533856d5
4 changed files with 21 additions and 19 deletions

View File

@ -93,7 +93,7 @@ class InitSettingsController extends Controller {
private function storeMailTemplates() { private function storeMailTemplates() {
$mails = InitialMails::retrieve(); $mails = InitialMails::retrieve();
foreach ($mails as $mailType => $mailLanguages) { foreach ($mails as $mailType => $mailLanguages) {
foreach ($mailLanguages as $mailLanguage => $mailContent) { foreach ($mailLanguages as $mailLanguage => $mailContent) {
$mailTemplate = new MailTemplate(); $mailTemplate = new MailTemplate();
@ -124,7 +124,7 @@ class InitSettingsController extends Controller {
} }
private function storeLanguages() { private function storeLanguages() {
$defaultLanguage = Controller::request('language'); $defaultLanguage = Controller::request('language');
foreach(Language::LANGUAGES as $languageCode) { foreach(Language::LANGUAGES as $languageCode) {
$language = new Language(); $language = new Language();
$language->setProperties([ $language->setProperties([

View File

@ -27,10 +27,10 @@ abstract class Controller {
} }
}; };
} }
public function validate() { public function validate() {
$validator = new Validator(); $validator = new Validator();
$validator->validate($this->validations()); $validator->validate($this->validations());
} }
@ -54,7 +54,7 @@ abstract class Controller {
public static function request($key, $secure = false) { public static function request($key, $secure = false) {
$result = call_user_func(self::$dataRequester, $key); $result = call_user_func(self::$dataRequester, $key);
if($secure) { if($secure) {
$config = HTMLPurifier_Config::createDefault(); $config = HTMLPurifier_Config::createDefault();
$purifier = new HTMLPurifier($config); $purifier = new HTMLPurifier($config);
@ -63,7 +63,7 @@ abstract class Controller {
return $result; return $result;
} }
} }
public static function getLoggedUser() { public static function getLoggedUser() {
$session = Session::getInstance(); $session = Session::getInstance();
@ -90,7 +90,7 @@ abstract class Controller {
public static function getAppInstance() { public static function getAppInstance() {
return \Slim\Slim::getInstance(); return \Slim\Slim::getInstance();
} }
public function uploadFile($forceUpload = false) { public function uploadFile($forceUpload = false) {
$allowAttachments = Setting::getSetting('allow-attachments')->getValue(); $allowAttachments = Setting::getSetting('allow-attachments')->getValue();
@ -114,8 +114,8 @@ abstract class Controller {
throw new Exception(ERRORS::INVALID_FILE); throw new Exception(ERRORS::INVALID_FILE);
} }
} }
public static function isUserSystemEnabled() { public static function isUserSystemEnabled() {
return Setting::getSetting('user-system-enabled')->getValue(); return Setting::getSetting('user-system-enabled')->getValue();
} }
} }

View File

@ -30,8 +30,8 @@ class Hashing {
$sqrt = sqrt($number); $sqrt = sqrt($number);
$prime = true; $prime = true;
for($i = 0; $i < $sqrt; $i++) { for($i = 2; $i <= $sqrt; $i++) {
if($sqrt % 2 === 0) { if($number % $i === 0) {
$prime = false; $prime = false;
break; break;
} }
@ -39,4 +39,4 @@ class Hashing {
return $prime; return $prime;
} }
} }

View File

@ -4,7 +4,7 @@ class LinearCongruentialGenerator {
private $first; private $first;
private $min = 100000; private $min = 100000;
private $max = 999999; private $max = 999999;
public function setRange($min, $max) { public function setRange($min, $max) {
$this->min = $min; $this->min = $min;
$this->max = $max; $this->max = $max;
@ -12,20 +12,22 @@ class LinearCongruentialGenerator {
public function setGap($gap) { public function setGap($gap) {
if(!Hashing::isPrime($gap)) throw new Exception('LinearCongruentialGenerator: gap must be prime'); if(!Hashing::isPrime($gap)) throw new Exception('LinearCongruentialGenerator: gap must be prime');
$this->gap = $gap; $this->gap = $gap;
} }
public function setFirst($first) { public function setFirst($first) {
$this->first = $first; $this->first = $first;
} }
public function generate($offset) { public function generate($offset) {
if($offset) return ($this->first - $this->min + $offset * $this->gap) % ($this->max - $this->min + 1) + $this->min; if(!$this->first) throw new Exception('LinearCongruentialGenerator: first is not set');
else return $this->generateFirst(); if(!$this->gap) throw new Exception('LinearCongruentialGenerator: gap is not set');
return ($this->first - $this->min + $offset * $this->gap) % ($this->max - $this->min + 1) + $this->min;
} }
public function generateFirst() { public function generateFirst() {
return Hashing::generateRandomNumber($this->min, $this->max); return Hashing::generateRandomNumber($this->min, $this->max);
} }
} }