diff --git a/4dev/tests/Create/CoreLibsCreateHashTest.php b/4dev/tests/Create/CoreLibsCreateHashTest.php index 24863635..c95f23c6 100644 --- a/4dev/tests/Create/CoreLibsCreateHashTest.php +++ b/4dev/tests/Create/CoreLibsCreateHashTest.php @@ -434,26 +434,47 @@ final class CoreLibsCreateHashTest extends TestCase return [ 'Empty array' => [ [], + null, '064f01fe', ], 'Simple array' => [ ['key' => 'value'], + null, '78f40899', ], 'flat array a' => [ [0, 1, 'b', 'c', 'a'], + null, 'ccec0f5b', ], 'flat array b' => [ ['b', 'c', 'a', 1, 0], + null, + 'c6040f5b', + ], + 'flat array b, not sorted' => [ + ['b', 'c', 'a', 1, 0], + false, 'c6040f5b', ], 'key array a' => [ ['A' => 0, 'F' => 1, 'C' => 'b', 'K' => 'c', 'B' => 'a'], + null, '01f11355', ], + 'key array a, not sorted' => [ + ['A' => 0, 'F' => 1, 'C' => 'b', 'K' => 'c', 'B' => 'a'], + false, + '04911355', + ], 'key array b' => [ ['A' => 0, 'B' => 'a', 'C' => 'b', 'F' => 1, 'K' => 'c'], + null, + '01f11355', + ], + 'key array b, not sorted' => [ + ['A' => 0, 'B' => 'a', 'C' => 'b', 'F' => 1, 'K' => 'c'], + false, '01f11355', ], 'complex nested array' => [ @@ -464,6 +485,7 @@ final class CoreLibsCreateHashTest extends TestCase ] ] ], + null, 'd8de1565', ] ]; @@ -479,12 +501,19 @@ final class CoreLibsCreateHashTest extends TestCase * @param array $input * @param string $expected */ - public function testGenerateImmutableHashForArray(array $input, string $expected): void + public function testGenerateImmutableHashForArray(array $input, ?bool $sort_keys, string $expected): void { - $this->assertEquals( - $expected, - \CoreLibs\Create\Hash::generateImmutableHashForArray($input) - ); + if ($sort_keys === null) { + $this->assertEquals( + $expected, + \CoreLibs\Create\Hash::generateImmutableHashForArray($input) + ); + } else { + $this->assertEquals( + $expected, + \CoreLibs\Create\Hash::generateImmutableHashForArray($input, $sort_keys) + ); + } } } diff --git a/www/admin/class_test.hash.php b/www/admin/class_test.hash.php index b721c543..7af85097 100644 --- a/www/admin/class_test.hash.php +++ b/www/admin/class_test.hash.php @@ -113,6 +113,7 @@ $tests = [ ]; foreach ($tests as $test) { print "IMMUTABLE HASH FOR ARRAY: " . Hash::generateImmutableHashForArray($test) . "
"; + print "IMMUTABLE HASH FOR ARRAY: " . Hash::generateImmutableHashForArray($test, sort_keys:false) . "
"; print "SERIALIZED HASH FOR ARRAY: " . hash(Hash::DEFAULT_HASH, serialize($test)) . "
"; } diff --git a/www/lib/CoreLibs/Create/Hash.php b/www/lib/CoreLibs/Create/Hash.php index ac1e29bd..5159e5f5 100644 --- a/www/lib/CoreLibs/Create/Hash.php +++ b/www/lib/CoreLibs/Create/Hash.php @@ -215,17 +215,22 @@ class Hash /** * generate a hash over any array data + * optional will not sort keys and so create hashes based on the original data. + * for correct array comapre it is recommended to keep sort keys on * * @param array $data + * @param bool $sort_keys [true] Whether to sort the array keys before hashing * @return string */ - public static function generateImmutableHashForArray(array $data): string + public static function generateImmutableHashForArray(array $data, bool $sort_keys = true): string { // Create a sorted copy to ensure consistent hashing // Generate hash using serialize for better accuracy return hash( self::DEFAULT_HASH, - serialize(ArrayHandler::createSortedArrayByKey($data)) + serialize( + $sort_keys ? ArrayHandler::createSortedArrayByKey($data) : $data + ) ); } }