Hash: immutable hash over array
with sorted keys, but arrays stay unsorted
This commit is contained in:
@@ -103,6 +103,19 @@ foreach ($hash_types as $hash_type) {
|
||||
// print "UNIQU ID SHORT : " . Hash::__uniqId() . "<br>";
|
||||
// print "UNIQU ID LONG : " . Hash::__uniqIdLong() . "<br>";
|
||||
|
||||
$tests = [
|
||||
// array diff
|
||||
[0, 1, 'b', 'c', 'a'],
|
||||
['b', 'c', 'a', 1, 0],
|
||||
// key set arrays DO NOT diff
|
||||
['A' => 0, 'F' => 1, 'C' => 'b', 'K' => 'c', 'B' => 'a'],
|
||||
['A' => 0, 'B' => 'a', 'C' => 'b', 'F' => 1, 'K' => 'c'],
|
||||
];
|
||||
foreach ($tests as $test) {
|
||||
print "IMMUTABLE HASH FOR ARRAY: " . Hash::generateImmutableHashForArray($test) . "<br>";
|
||||
print "SERIALIZED HASH FOR ARRAY: " . hash(Hash::DEFAULT_HASH, serialize($test)) . "<br>";
|
||||
}
|
||||
|
||||
print "</body></html>";
|
||||
|
||||
// __END__
|
||||
|
||||
@@ -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__
|
||||
|
||||
Reference in New Issue
Block a user