From 0109a67b2026d96e35f09edfdef896dd189ce32b Mon Sep 17 00:00:00 2001 From: Clemens Schwaighofer Date: Mon, 21 Feb 2022 06:50:45 +0900 Subject: [PATCH] Change Random key base to be string not array Because we do not have double byte characters in there we use a string so we don't have to do any array work. Return is also a string and not an array that is then converted to string. Add info to Colors Class for oklab code that we should use as basic for all conversions --- phpstan.neon | 1 + www/admin/class_test.randomkey.php | 2 ++ www/lib/CoreLibs/Convert/Colors.php | 3 +++ www/lib/CoreLibs/Create/RandomKey.php | 22 +++++++++++++--------- 4 files changed, 19 insertions(+), 9 deletions(-) diff --git a/phpstan.neon b/phpstan.neon index c9f147be..d5448c07 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -4,6 +4,7 @@ includes: parameters: tmpDir: /tmp/phpstan-corelibs level: 8 # max is now 9 + checkMissingCallableSignature: true paths: - %currentWorkingDirectory%/www bootstrapFiles: diff --git a/www/admin/class_test.randomkey.php b/www/admin/class_test.randomkey.php index de335b20..c91ffcd4 100644 --- a/www/admin/class_test.randomkey.php +++ b/www/admin/class_test.randomkey.php @@ -50,11 +50,13 @@ print '
Class Test Master
'; $key_length = 10; $key_length_b = 5; +$key_lenght_long = 64; print "S::RANDOMKEYGEN(auto): " . RandomKey::randomKeyGen() . "
"; print "S::SETRANDOMKEYLENGTH($key_length): " . RandomKey::setRandomKeyLength($key_length) . "
"; print "S::RANDOMKEYGEN($key_length): " . RandomKey::randomKeyGen() . "
"; print "S::RANDOMKEYGEN($key_length_b): " . RandomKey::randomKeyGen($key_length_b) . "
"; print "S::RANDOMKEYGEN($key_length): " . RandomKey::randomKeyGen() . "
"; +print "S::RANDOMKEYGEN($key_lenght_long): " . RandomKey::randomKeyGen($key_lenght_long) . "
"; $_array = new CoreLibs\Create\RandomKey(); print "C->RANDOMKEYGEN(auto): " . $_array->randomKeyGen() . "
"; // DEPRECATED diff --git a/www/lib/CoreLibs/Convert/Colors.php b/www/lib/CoreLibs/Convert/Colors.php index f565ebda..7e519357 100644 --- a/www/lib/CoreLibs/Convert/Colors.php +++ b/www/lib/CoreLibs/Convert/Colors.php @@ -10,6 +10,9 @@ * hsl to rgb */ +// TODO: use oklab as base for converting colors +// https://bottosson.github.io/posts/oklab/ + declare(strict_types=1); namespace CoreLibs\Convert; diff --git a/www/lib/CoreLibs/Create/RandomKey.php b/www/lib/CoreLibs/Create/RandomKey.php index 80c476c7..e3c7aa32 100644 --- a/www/lib/CoreLibs/Create/RandomKey.php +++ b/www/lib/CoreLibs/Create/RandomKey.php @@ -11,8 +11,8 @@ namespace CoreLibs\Create; class RandomKey { // key generation - /** @var array */ - private static $key_range = []; + /** @var string */ + private static $key_range = ''; /** @var int */ private static $one_key_length; /** @var int */ @@ -33,9 +33,13 @@ class RandomKey */ private static function initRandomKeyData() { - // random key generation - self::$key_range = array_merge(range('A', 'Z'), range('a', 'z'), range('0', '9')); - self::$one_key_length = count(self::$key_range); + // random key generation base string + self::$key_range = join('', array_merge( + range('A', 'Z'), + range('a', 'z'), + range('0', '9') + )); + self::$one_key_length = strlen(self::$key_range); } /** @@ -103,11 +107,11 @@ class RandomKey } else { $use_key_length = self::$key_length; } - - $pieces = []; + // create random string + $random_string = ''; for ($i = 1; $i <= $use_key_length; $i++) { - $pieces[] = self::$key_range[random_int(0, self::$one_key_length - 1)]; + $random_string .= self::$key_range[random_int(0, self::$one_key_length - 1)]; } - return join('', $pieces); + return $random_string; } }