Skip to content

Instantly share code, notes, and snippets.

@askaaqib
Created December 4, 2020 06:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save askaaqib/9a7deac6c0b3cb309a907e27941c6359 to your computer and use it in GitHub Desktop.
Save askaaqib/9a7deac6c0b3cb309a907e27941c6359 to your computer and use it in GitHub Desktop.
Merkle Root in PHP Example
<?php
$array = ["spoiler", "kirk", "category", "brand"];
$hashes = [];
$hash = "";
foreach ($array as $key => $value) {
$hashes[] = hash("sha256", $value);
}
$count = count($hashes) - 1;
while (true) {
$temp = [];
for ($i = 0; $i <= count($hashes)-1; $i+=2) {
$temp[] = hash("sha256", $hashes[$i] . $hashes[$i+1]);
}
$hashes = $temp;
if (count($hashes)==1) {
break;
}
}
echo "Root is: " . $hashes[0];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment