diff --git a/4dev/tests/Create/CoreLibsCreateRandomKeyTest.php b/4dev/tests/Create/CoreLibsCreateRandomKeyTest.php index 3662aa65..e2714008 100644 --- a/4dev/tests/Create/CoreLibsCreateRandomKeyTest.php +++ b/4dev/tests/Create/CoreLibsCreateRandomKeyTest.php @@ -13,32 +13,6 @@ use PHPUnit\Framework\TestCase; */ final class CoreLibsCreateRandomKeyTest extends TestCase { - /** - * Undocumented function - * - * @return array - */ - public function keyLenghtProvider(): array - { - return [ - 'valid key length' => [ - 0 => 6, - 1 => true, - 2 => 6, - ], - 'negative key length' => [ - 0 => -1, - 1 => false, - 2 => 4, - ], - 'tpp big key length' => [ - 0 => 300, - 1 => false, - 2 => 4, - ], - ]; - } - /** * Undocumented function * @@ -47,109 +21,60 @@ final class CoreLibsCreateRandomKeyTest extends TestCase public function randomKeyGenProvider(): array { return [ - 'default key length' => [ + // just key length + 'default key length, default char set' => [ 0 => null, - 1 => 4 + 1 => \CoreLibs\Create\RandomKey::KEY_LENGTH_DEFAULT ], - 'set -1 key length default' => [ + 'set -1 key length, default char set' => [ 0 => -1, - 1 => 4, + 1 => \CoreLibs\Create\RandomKey::KEY_LENGTH_DEFAULT, ], - 'set too large key length' => [ + 'set 0 key length, default char set' => [ + 0 => -1, + 1 => \CoreLibs\Create\RandomKey::KEY_LENGTH_DEFAULT, + ], + 'set too large key length, default char set' => [ 0 => 300, - 1 => 4, + 1 => \CoreLibs\Create\RandomKey::KEY_LENGTH_DEFAULT, ], - 'set override key lenght' => [ + 'set override key lenght, default char set' => [ 0 => 6, 1 => 6, ], + // just character set + 'default key length, different char set A' => [ + 0 => \CoreLibs\Create\RandomKey::KEY_LENGTH_DEFAULT, + 1 => \CoreLibs\Create\RandomKey::KEY_LENGTH_DEFAULT, + 2 => [ + 'A', 'B', 'C' + ], + ], + 'different key length, different char set B' => [ + 0 => 16, + 1 => 16, + 2 => [ + 'A', 'B', 'C' + ], + 3 => [ + '1', '2', '3' + ] + ], ]; } - /** - * 1 - * - * @return array - */ - public function keepKeyLengthProvider(): array + // Alternative more efficient version using strpos + private function allCharsInSet(string $input, string $allowedChars): bool { - return [ - 'set too large' => [ - 0 => 6, - 1 => 300, - 2 => 6, - ], - 'set too small' => [ - 0 => 8, - 1 => -2, - 2 => 8, - ], - 'change valid' => [ - 0 => 10, - 1 => 6, - 2 => 6, - ] - ]; - } + $inputLength = strlen($input); - /** - * run before each test and reset to default 4 - * - * @before - * - * @return void - */ - public function resetKeyLength(): void - { - \CoreLibs\Create\RandomKey::setRandomKeyLength(4); - } - - /** - * check that first length is 4 - * - * @covers ::getRandomKeyLength - * @testWith [4] - * @testdox getRandomKeyLength on init will be $expected [$_dataName] - * - * @param integer $expected - * @return void - */ - public function testGetRandomKeyLengthInit(int $expected): void - { - $this->assertEquals( - $expected, - \CoreLibs\Create\RandomKey::getRandomKeyLength() - ); - } - - /** - * Undocumented function - * - * @covers ::setRandomKeyLength - * @covers ::getRandomKeyLength - * @dataProvider keyLenghtProvider - * @testdox setRandomKeyLength $input will be $expected, compare to $compare [$_dataName] - * - * @param integer $input - * @param boolean $expected - * @param integer $compare - * @return void - */ - public function testSetRandomKeyLength(int $input, bool $expected, int $compare): void - { - // set - $this->assertEquals( - $expected, - \CoreLibs\Create\RandomKey::setRandomKeyLength($input) - ); - // read test, if false, use compare check - if ($expected === false) { - $input = $compare; + for ($i = 0; $i < $inputLength; $i++) { + if (strpos($allowedChars, $input[$i]) === false) { + return false; + } } - $this->assertEquals( - $input, - \CoreLibs\Create\RandomKey::getRandomKeyLength() - ); + + return true; } /** @@ -163,43 +88,41 @@ final class CoreLibsCreateRandomKeyTest extends TestCase * @param integer $expected * @return void */ - public function testRandomKeyGen(?int $input, int $expected): void + public function testRandomKeyGen(?int $input, int $expected, array ...$key_range): void { + $__key_data = \CoreLibs\Create\RandomKey::KEY_CHARACTER_RANGE_DEFAULT; + if (count($key_range)) { + $__key_data = join('', array_unique(array_merge(...$key_range))); + } if ($input === null) { $this->assertEquals( $expected, strlen(\CoreLibs\Create\RandomKey::randomKeyGen()) ); - } else { + } elseif ($input !== null && !count($key_range)) { + $random_key = \CoreLibs\Create\RandomKey::randomKeyGen($input); + $this->assertTrue( + $this->allCharsInSet($random_key, $__key_data), + 'Characters not valid' + ); $this->assertEquals( $expected, - strlen(\CoreLibs\Create\RandomKey::randomKeyGen($input)) + strlen($random_key), + 'String length not matching' + ); + } elseif (count($key_range)) { + $random_key = \CoreLibs\Create\RandomKey::randomKeyGen($input, ...$key_range); + $this->assertTrue( + $this->allCharsInSet($random_key, $__key_data), + 'Characters not valid' + ); + $this->assertEquals( + $expected, + strlen($random_key), + 'String length not matching' ); } } - - /** - * Check that if set to n and then invalid, it keeps the previous one - * or if second change valid, second will be shown - * - * @covers ::setRandomKeyLength - * @dataProvider keepKeyLengthProvider - * @testdox keep setRandomKeyLength set with $input_valid and then $input_invalid will be $expected [$_dataName] - * - * @param integer $input_valid - * @param integer $input_invalid - * @param integer $expected - * @return void - */ - public function testKeepKeyLength(int $input_valid, int $input_invalid, int $expected): void - { - \CoreLibs\Create\RandomKey::setRandomKeyLength($input_valid); - \CoreLibs\Create\RandomKey::setRandomKeyLength($input_invalid); - $this->assertEquals( - $expected, - \CoreLibs\Create\RandomKey::getRandomKeyLength() - ); - } } // __END__ diff --git a/www/admin/class_test.randomkey.php b/www/admin/class_test.randomkey.php index 4416b63d..c0c531a3 100644 --- a/www/admin/class_test.randomkey.php +++ b/www/admin/class_test.randomkey.php @@ -38,13 +38,21 @@ $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::SETRANDOMKEYLENGTH($key_length): " . RandomKey::setRandomKeyLength($key_length) . "
"; +print "S::RANDOMKEYGEN($key_length): " . RandomKey::randomKeyGen($key_length) . "
"; print "S::RANDOMKEYGEN($key_length_b): " . RandomKey::randomKeyGen($key_length_b) . "
"; -print "S::RANDOMKEYGEN($key_length): " . RandomKey::randomKeyGen() . "
"; +print "S::RANDOMKEYGEN($key_length): " . RandomKey::randomKeyGen($key_length) . "
"; print "S::RANDOMKEYGEN($key_lenght_long): " . RandomKey::randomKeyGen($key_lenght_long) . "
"; +print "S::RANDOMKEYGEN($key_lenght_long, list data): " + . RandomKey::randomKeyGen($key_lenght_long, ['A', 'B', 'C'], ['7', '8', '9']) . "
"; +print "S::RANDOMKEYGEN(auto): " . RandomKey::randomKeyGen() . "
"; +print "===
"; $_array = new CoreLibs\Create\RandomKey(); -print "C->RANDOMKEYGEN(auto): " . $_array->randomKeyGen() . "
"; +print "C->RANDOMKEYGEN(default): " . $_array->randomKeyGen() . "
"; +print "===
"; +// CHANGE key characters +$_array = new CoreLibs\Create\RandomKey(['A', 'F', 'B'], ['1', '5', '9']); +print "C->RANDOMKEYGEN(pre set): " . $_array->randomKeyGen() . "
"; print ""; diff --git a/www/lib/CoreLibs/Create/RandomKey.php b/www/lib/CoreLibs/Create/RandomKey.php index 6d57f754..116e1c21 100644 --- a/www/lib/CoreLibs/Create/RandomKey.php +++ b/www/lib/CoreLibs/Create/RandomKey.php @@ -10,37 +10,91 @@ namespace CoreLibs\Create; class RandomKey { + /** @var int set the default key length it nothing else is set */ + public const int KEY_LENGTH_DEFAULT = 4; + /** @var int the maximum key length allowed */ + public const int KEY_LENGTH_MAX = 256; + /** @var string the default characters in the key range */ + public const string KEY_CHARACTER_RANGE_DEFAULT = + 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' + . 'abcdefghijklmnopqrstuvwxyz' + . '0123456789'; // key generation - /** @var string */ - private static string $key_range = ''; - /** @var int */ - private static int $one_key_length; - /** @var int */ - private static int $key_length = 4; // default key length - /** @var int */ - private static int $max_key_length = 256; // max allowed length + /** @var string all the characters that are int he current radnom key range */ + private static string $key_character_range = ''; + /** @var int character count in they key character range */ + private static int $key_character_range_length = 0; + /** @var int default key lenghth */ + /** @deprecated Will be removed */ + private static int $key_length = 4; /** * if launched as class, init random key data first */ - public function __construct() + public function __construct(array ...$key_range) { - $this->initRandomKeyData(); + $this->setRandomKeyData(...$key_range); } + + /** + * internal key range validation + * + * @param array> ...$key_range + * @return string + */ + private static function validateRandomKeyData(array ...$key_range): string + { + $key_character_range = join('', array_unique(array_merge(...$key_range))); + if (strlen(self::$key_character_range) <= 1) { + return ''; + } + return $key_character_range; + } + /** * sets the random key range with the default values * + * @param array> $key_range a list of key ranges as array * @return void has no return + * @throws \LengthException If the string length is only 1 abort */ - private static function initRandomKeyData(): void + public static function setRandomKeyData(array ...$key_range): void { - // 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); + // if key range is not set + if (!count($key_range)) { + self::$key_character_range = self::KEY_CHARACTER_RANGE_DEFAULT; + } else { + self::$key_character_range = self::validateRandomKeyData(...$key_range); + // random key generation base string + } + self::$key_character_range_length = strlen(self::$key_character_range); + if (self::$key_character_range_length <= 1) { + throw new \LengthException( + "The given key character range '" . self::$key_character_range . "' " + . "is too small, must be at lest two characters: " + . self::$key_character_range_length + ); + } + } + + /** + * get the characters for the current key characters + * + * @return string + */ + public static function getRandomKeyData(): string + { + return self::$key_character_range; + } + + /** + * get the length of all random characters + * + * @return int + */ + public static function getRandomKeyDataLength(): int + { + return self::$key_character_range_length; } /** @@ -53,7 +107,7 @@ class RandomKey { if ( $key_length > 0 && - $key_length <= self::$max_key_length + $key_length <= self::KEY_LENGTH_MAX ) { return true; } else { @@ -67,6 +121,7 @@ class RandomKey * * @param int $key_length key length * @return bool true/false for set status + * @deprecated This function does no longer set the key length, the randomKeyGen parameter has to b used */ public static function setRandomKeyLength(int $key_length): bool { @@ -83,6 +138,7 @@ class RandomKey * get the current set random key length * * @return int Current set key length + * @deprecated Key length is set during randomKeyGen call, this nethid is deprecated */ public static function getRandomKeyLength(): int { @@ -94,28 +150,37 @@ class RandomKey * if override key length is set, it will check on valid key and use this * this will not set the class key length variable * - * @param int $key_length key length override, -1 for use default - * @return string random key + * @param int $key_length [default=-1] key length override, + * if not set use default [LEGACY] + * @param array> $key_range a list of key ranges as array, + * if not set use previous set data + * @return string random key */ - public static function randomKeyGen(int $key_length = -1): string - { - // init random key strings if not set - if ( - !isset(self::$one_key_length) - ) { - self::initRandomKeyData(); - } - $use_key_length = 0; - // only if valid int key with valid length - if (self::validateRandomKeyLenght($key_length) === true) { - $use_key_length = $key_length; + public static function randomKeyGen( + int $key_length = self::KEY_LENGTH_DEFAULT, + array ...$key_range + ): string { + $key_character_range = ''; + if (count($key_range)) { + $key_character_range = self::validateRandomKeyData(...$key_range); + $key_character_range_length = strlen($key_character_range); } else { - $use_key_length = self::$key_length; + if (!self::$key_character_range_length) { + self::setRandomKeyData(); + } + $key_character_range = self::getRandomKeyData(); + $key_character_range_length = self::getRandomKeyDataLength(); + } + // if not valid key length, fallback to default + if (!self::validateRandomKeyLenght($key_length)) { + $key_length = self::KEY_LENGTH_DEFAULT; } // create random string $random_string = ''; - for ($i = 1; $i <= $use_key_length; $i++) { - $random_string .= self::$key_range[random_int(0, self::$one_key_length - 1)]; + for ($i = 1; $i <= $key_length; $i++) { + $random_string .= $key_character_range[ + random_int(0, $key_character_range_length - 1) + ]; } return $random_string; }