diff --git a/4dev/tests/Create/CoreLibsCreateRandomKeyTest.php b/4dev/tests/Create/CoreLibsCreateRandomKeyTest.php index 3662aa65..508f7ff9 100644 --- a/4dev/tests/Create/CoreLibsCreateRandomKeyTest.php +++ b/4dev/tests/Create/CoreLibsCreateRandomKeyTest.php @@ -200,6 +200,47 @@ final class CoreLibsCreateRandomKeyTest extends TestCase \CoreLibs\Create\RandomKey::getRandomKeyLength() ); } + + /** + * 1 + * + * @return array + */ + public function setRandomKeyCharactersProvider(): array + { + return [ + 'lower case characters' => [ + 0 => "/^[a-z]+$/", + 1 => ["a", "z"], + ], + 'lower case and upper case' => [ + 0 => "/^[A-Za-z]+$/", + 1 => ["A", "Z"], + 1 => ["a", "z"], + ], + 'alphanumeric' => [ + 0 => "/^[A-Za-z0-9]+$/", + 1 => ["A", "Z"], + 1 => ["a", "z"], + 1 => ["0", "9"], + ] + ]; + } + + + /** + * Undocumented function + * + * @covers ::setRandomKeyCharacters + * @dataProvider setRandomKeyCharactersProvider + * @testdox check setRandomKeyCharacters [$_dataName] + * + * @return void + */ + public function testSetRandomKeyCharacters(string $expected_regex, array ...$random_keys): void + { + + } } // __END__ diff --git a/www/admin/class_test.randomkey.php b/www/admin/class_test.randomkey.php index 4416b63d..c6c11daf 100644 --- a/www/admin/class_test.randomkey.php +++ b/www/admin/class_test.randomkey.php @@ -43,8 +43,10 @@ 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() . "
"; +$random_key = new CoreLibs\Create\RandomKey(); +print "C->RANDOMKEYGEN(auto): " . $random_key->randomKeyGen() . "
"; +$random_key->setRandomKeyCharacters(['a', 'z']); +print "C->RANDOMKEYGEN(Fixed set): " . $random_key->randomKeyGen() . "
"; print ""; diff --git a/www/lib/CoreLibs/Create/RandomKey.php b/www/lib/CoreLibs/Create/RandomKey.php index 6d57f754..ed405fe4 100644 --- a/www/lib/CoreLibs/Create/RandomKey.php +++ b/www/lib/CoreLibs/Create/RandomKey.php @@ -61,6 +61,32 @@ class RandomKey } } + /** + * sets a different character range + * + * @param array $ranges + * @return bool + */ + public static function setRandomKeyCharacters(array ...$ranges): bool + { + // each range must be an arary of start end range + $character_range = []; + foreach ($ranges as $range) { + if (count($range) != 2) { + throw new \ArgumentCountError(__FUNCTION__ . ' array must have two range entries'); + } + $character_range = array_merge($character_range, range($range[0], $range[1])); + } + self::$key_range = join('', $character_range); + self::$one_key_length = strlen(self::$key_range); + if ( + self::$one_key_length == 0 + ) { + return false; + } + return true; + } + /** * sets the key length and checks that they key given is valid * if failed it will not change the default key length and return false