Hash: immutable hash over array

with sorted keys, but arrays stay unsorted
This commit is contained in:
2026-09-18 18:36:40 +09:00
parent 2bb405d741
commit 0b057864ae
3 changed files with 96 additions and 0 deletions
+18
View File
@@ -8,6 +8,8 @@ declare(strict_types=1);
namespace CoreLibs\Create;
use CoreLibs\Combined\ArrayHandler;
class Hash
{
/** @var string default short hash -> deprecated use STANDARD_HASH_SHORT */
@@ -210,6 +212,22 @@ class Hash
{
return self::hash($string, self::STANDARD_HASH);
}
/**
* generate a hash over any array data
*
* @param array<mixed> $data
* @return string
*/
public static function generateImmutableHashForArray(array $data): 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))
);
}
}
// __END__