This is a very simple implemenation of merkle tree in which you can use multiple hashes to create a merkele tree and compress it to a single hash .
You can install the package via composer:
composer require mohamedahmed01/simple-merkele
//include SimpleMereke
//prepare your array of hashes
$hashsArray=[
hash('sha256', 'hello'),
hash('sha256', 'goodbye'),
hash('sha256', 'topofthemorning'),
hash('sha256', 'sionara'),
hash('sha256', 'areviditche'),
hash('sha256', 'arregato'),
];
//create new instance of simpleMerkele
$merkele = new SimpleMerkele();
//add your hashes to the inventory
foreach ($hashsArray as $hash) {
$merkele->addHash($hash);
}
//create the top hash
$merkele->calculateTree(); //9b8dd5dd1f56d5fa17a67c10b8891c57e51f5fd36fe3a2d7e290d605840332d8
$merkele->resetTree();
Method | Description |
---|---|
SimpleMerkele($mode,$algo) | Creates new instance with the specified configuration |
addHash($hash) | add hash to the internal inventory not that once added cannot be removed |
calculateTree() | create the tree and the top node i.e hash |
resetTree() | reset the internal memories to be able to add new hashes and create new tree |
Flag/Param | Description |
---|---|
SimpleMerkele::ALLOW_ODD_ARRAYS | Allow odd length trees but adjusting the pairing internally |
SimpleMerkele::DIS_ALLOW_ODD_ARRAYS | throws an exceptions on odd length tree |
$algo | accept standard algo type i.e "md5","sha256" refer to HashValidation class for full list |
composer test
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.