diff --git a/4dev/tests/CoreLibsCreateHashTest.php b/4dev/tests/CoreLibsCreateHashTest.php index c8866afe..0ef7393b 100644 --- a/4dev/tests/CoreLibsCreateHashTest.php +++ b/4dev/tests/CoreLibsCreateHashTest.php @@ -110,15 +110,6 @@ final class CoreLibsCreateHashTest extends TestCase ]; } - public function uniqIdLongProvider(): array - { - return [ - 'uniq id long: ' . \CoreLibs\Create\Hash::STANDARD_HASH_LONG => [ - strlen(hash(\CoreLibs\Create\Hash::STANDARD_HASH_LONG, 'A')) - ], - ]; - } - /** * Undocumented function * @@ -194,24 +185,6 @@ final class CoreLibsCreateHashTest extends TestCase } } - /** - * Undocumented function - * - * @covers ::__uniqueId - * @testWith [8] - * @testdox __uniqId will be length $expected [$_dataName] - * - * @param integer $expected - * @return void - */ - public function testUniqId(int $expected): void - { - $this->assertEquals( - $expected, - strlen(\CoreLibs\Create\Hash::__uniqId()) - ); - } - /** * Undocumented function * @@ -230,24 +203,6 @@ final class CoreLibsCreateHashTest extends TestCase \CoreLibs\Create\Hash::__hashLong($input) ); } - - /** - * Undocumented function - * - * @covers ::__uniqueIdLong - * @dataProvider uniqIdLongProvider - * @testdox __uniqIdLong will be length $expected [$_dataName] - * - * @param integer $expected - * @return void - */ - public function testUniqIdLong(int $expected): void - { - $this->assertEquals( - $expected, - strlen(\CoreLibs\Create\Hash::__uniqIdLong()) - ); - } } // __END__ diff --git a/4dev/tests/CoreLibsCreateUidsTest.php b/4dev/tests/CoreLibsCreateUidsTest.php index 1a5fcd9f..a914cb93 100644 --- a/4dev/tests/CoreLibsCreateUidsTest.php +++ b/4dev/tests/CoreLibsCreateUidsTest.php @@ -13,6 +13,11 @@ use PHPUnit\Framework\TestCase; */ final class CoreLibsCreateUidsTest extends TestCase { + /** + * Undocumented function + * + * @return array + */ public function uniqIdProvider(): array { return [ @@ -32,6 +37,10 @@ final class CoreLibsCreateUidsTest extends TestCase 0 => 'adler32', 1 => 8 ], + 'not in list hash but valid' => [ + 0 => 'sha3-512', + 1 => strlen(hash('sha3-512', 'A')) + ], 'default hash not set' => [ 0 => null, 1 => 64, @@ -39,10 +48,37 @@ final class CoreLibsCreateUidsTest extends TestCase 'invalid name' => [ 0 => 'iamnotavalidhash', 1 => 64, - ] + ], + 'auto: ' . \CoreLibs\Create\Uids::DEFAULT_HASH => [ + 0 => \CoreLibs\Create\Uids::DEFAULT_HASH, + 1 => strlen(hash(\CoreLibs\Create\Uids::DEFAULT_HASH, 'A')) + ], + 'auto: ' . \CoreLibs\Create\Uids::STANDARD_HASH_LONG => [ + 0 => \CoreLibs\Create\Uids::STANDARD_HASH_LONG, + 1 => strlen(hash(\CoreLibs\Create\Uids::STANDARD_HASH_LONG, 'A')) + ], + 'auto: ' . \CoreLibs\Create\Uids::STANDARD_HASH_SHORT => [ + 0 => \CoreLibs\Create\Uids::STANDARD_HASH_SHORT, + 1 => strlen(hash(\CoreLibs\Create\Uids::STANDARD_HASH_SHORT, 'A')) + ], ]; } + /** + * Undocumented function + * + * @return array + */ + public function uniqIdLongProvider(): array + { + return [ + 'uniq id long: ' . \CoreLibs\Create\Uids::STANDARD_HASH_LONG => [ + strlen(hash(\CoreLibs\Create\Uids::STANDARD_HASH_LONG, 'A')) + ], + ]; + } + + /** * must match 7e78fe0d-59b8-4637-af7f-e88d221a7d1e * @@ -109,6 +145,42 @@ final class CoreLibsCreateUidsTest extends TestCase strlen(\CoreLibs\Create\Uids::uniqId()) ); } + + /** + * Short id, always 8 in length + * + * @covers ::uniqIdShort + * @testWith [8] + * @testdox uniqIdShort will be length $expected [$_dataName] + * + * @param integer $expected + * @return void + */ + public function testUniqIdShort(int $expected): void + { + $this->assertEquals( + $expected, + strlen(\CoreLibs\Create\Uids::uniqIdShort()) + ); + } + + /** + * Long Id, length can change + * + * @covers ::uniqIdLong + * @dataProvider uniqIdLongProvider + * @testdox uniqIdLong will be length $expected [$_dataName] + * + * @param integer $expected + * @return void + */ + public function testUniqIdLong(int $expected): void + { + $this->assertEquals( + $expected, + strlen(\CoreLibs\Create\Uids::uniqIdLong()) + ); + } } // __END__ diff --git a/www/admin/class_test.hash.php b/www/admin/class_test.hash.php index c515c932..fb96b9a6 100644 --- a/www/admin/class_test.hash.php +++ b/www/admin/class_test.hash.php @@ -68,8 +68,8 @@ print "
CURRENT STANDARD_HASH_LONG: " . Hash::STANDARD_HASH_LONG . "
"; print "HASH SHORT: " . $to_crc . ": " . Hash::__hash($to_crc) . "
"; print "HASH LONG: " . $to_crc . ": " . Hash::__hashLong($to_crc) . "
"; -print "UNIQU ID SHORT : " . Hash::__uniqId() . "
"; -print "UNIQU ID LONG : " . Hash::__uniqIdLong() . "
"; +// print "UNIQU ID SHORT : " . Hash::__uniqId() . "
"; +// print "UNIQU ID LONG : " . Hash::__uniqIdLong() . "
"; // DEPRECATED /* print "D/__CRC32B: $to_crc: ".$basic->__crc32b($to_crc)."
"; diff --git a/www/lib/CoreLibs/Create/Hash.php b/www/lib/CoreLibs/Create/Hash.php index e6169e8d..4a52659c 100644 --- a/www/lib/CoreLibs/Create/Hash.php +++ b/www/lib/CoreLibs/Create/Hash.php @@ -91,10 +91,13 @@ class Hash * create a unique id with the standard hash type defined in __hash * * @return string Unique ID with fixed length of 8 characters + * @deprecated Use \CoreLibs\Create\Uids::uniqIdShort() instead */ public static function __uniqId(): string { - return self::__hash(uniqid((string)rand(), true)); + trigger_error('Method ' . __METHOD__ . ' is deprecated, ' + . '\CoreLibs\Create\Uids::uniqIdShort() class', E_USER_DEPRECATED); + return \CoreLibs\Create\Uids::uniqIdShort(); } /** @@ -102,10 +105,13 @@ class Hash * defined in __hashLong * * @return string Unique ID with length of current default long hash + * @deprecated Use \CoreLibs\Create\Uids::uniqIdLong() instead */ public static function __uniqIdLong(): string { - return self::__hashLong(uniqid((string)rand(), true)); + trigger_error('Method ' . __METHOD__ . ' is deprecated, ' + . '\CoreLibs\Create\Uids::uniqIdLong() class', E_USER_DEPRECATED); + return \CoreLibs\Create\Uids::uniqIdLong(); } } diff --git a/www/lib/CoreLibs/Create/Uids.php b/www/lib/CoreLibs/Create/Uids.php index 38c3354b..a4b9e92a 100644 --- a/www/lib/CoreLibs/Create/Uids.php +++ b/www/lib/CoreLibs/Create/Uids.php @@ -7,7 +7,9 @@ namespace CoreLibs\Create; class Uids { // what to use as a default hash if non ise set and no DEFAULT_HASH is defined - public const FALLBACK_HASH = 'sha256'; + public const DEFAULT_HASH = 'sha256'; + public const STANDARD_HASH_LONG = 'ripemd160'; + public const STANDARD_HASH_SHORT = 'adler32'; /** * creates psuedo random uuid v4 @@ -53,23 +55,52 @@ class Uids case 'md5': $uniq_id = md5(uniqid((string)rand(), true)); break; - case 'sha256': - $uniq_id = hash('sha256', uniqid((string)rand(), true)); + case self::DEFAULT_HASH: + $uniq_id = hash(self::DEFAULT_HASH, uniqid((string)rand(), true)); break; - case 'ripemd160': - $uniq_id = hash('ripemd160', uniqid((string)rand(), true)); + case self::STANDARD_HASH_LONG: + $uniq_id = hash(self::STANDARD_HASH_LONG, uniqid((string)rand(), true)); break; - case 'adler32': - $uniq_id = hash('adler32', uniqid((string)rand(), true)); + case self::STANDARD_HASH_SHORT: + $uniq_id = hash(self::STANDARD_HASH_SHORT, uniqid((string)rand(), true)); break; default: - // fallback to this hash type - $hash = self::FALLBACK_HASH; + // if not empty, check if in valid list + if ( + !empty($type) && + in_array($type, hash_algos()) + ) { + $hash = $type; + } else { + // fallback to default hash type if none set or invalid + $hash = self::DEFAULT_HASH; + } $uniq_id = hash($hash, uniqid((string)rand(), true)); break; } return $uniq_id; } + + /** + * create a unique id with the standard hash type defined in __hash + * + * @return string Unique ID with fixed length of 8 characters + */ + public static function uniqIdShort(): string + { + return self::uniqId(self::STANDARD_HASH_SHORT); + } + + /** + * create a unique id with the standard long hash type + * defined in __hashLong + * + * @return string Unique ID with length of current default long hash + */ + public static function uniqIdLong(): string + { + return self::uniqId(self::STANDARD_HASH_LONG); + } } // __END__ diff --git a/www/lib/CoreLibs/Debug/Logging.php b/www/lib/CoreLibs/Debug/Logging.php index 786ba37d..5dfe77cb 100644 --- a/www/lib/CoreLibs/Debug/Logging.php +++ b/www/lib/CoreLibs/Debug/Logging.php @@ -25,7 +25,7 @@ declare(strict_types=1); namespace CoreLibs\Debug; use CoreLibs\Debug\Support; -use CoreLibs\Create\Hash; +use CoreLibs\Create\Uids; use CoreLibs\Get\System; use CoreLibs\Convert\Html; @@ -161,7 +161,7 @@ class Logging // running time start for script $this->script_starttime = microtime(true); // set per run UID for logging - $this->running_uid = Hash::__uniqId(); + $this->running_uid = Uids::uniqIdShort(); // set the page name $this->page_name = System::getPageName(); // set host name diff --git a/www/lib/CoreLibs/Output/Form/Generate.php b/www/lib/CoreLibs/Output/Form/Generate.php index 3686aa3b..e2d0befd 100644 --- a/www/lib/CoreLibs/Output/Form/Generate.php +++ b/www/lib/CoreLibs/Output/Form/Generate.php @@ -2246,17 +2246,17 @@ class Generate extends \CoreLibs\DB\Extended\ArrayIO // if drop down db read data for element list from the given sub table as from the query // only two elements are allowed: pos 0 is key, pso 1 is visible output name if (isset($data_array['type']) && $data_array['type'] == 'drop_down_db') { - $md_q = md5($data_array['query']); while (is_array($res = $this->dbReturn($data_array['query']))) { /** @phan-suppress-next-line PhanTypeInvalidDimOffset */ - $this->log->debug('edit', 'Q[' . $md_q . '] pos: ' . $this->cursor_ext[$md_q]['pos'] + $this->log->debug('edit', 'Q[' . $this->dbGetQueryHash($data_array['query']) . '] pos: ' + . $this->dbGetCursorExt($data_array['query'], 'pos') . ' | want: ' . ($data_array['preset'] ?? '-') . ' | set: ' . ($data['preset'][$el_name] ?? '-')); // first is default for this element if ( isset($data_array['preset']) && (!isset($data['preset'][$el_name]) || empty($data['preset'][$el_name])) && - ($this->cursor_ext[$md_q]['pos'] == $data_array['preset']) + ($this->dbGetCursorExt($data_array['query'], 'pos') == $data_array['preset']) ) { $data['preset'][$el_name] = $res[0]; }