Created
December 4, 2020 06:05
-
-
Save askaaqib/9a7deac6c0b3cb309a907e27941c6359 to your computer and use it in GitHub Desktop.
Merkle Root in PHP Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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