generated from olivmath/template
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
26 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import hashlib | ||
|
||
def hash_function(data): | ||
return hashlib.sha256(data.encode()).hexdigest() | ||
|
||
def merkle_tree_root(leaves): | ||
# Converte as folhas para hashed leaves se necessário | ||
hashed_leaves = [hash_function(leaf) for leaf in leaves] | ||
|
||
# Constrói as camadas da árvore | ||
while len(hashed_leaves) > 1: | ||
# Se o número de nós for ímpar, duplica o último nó | ||
if len(hashed_leaves) % 2 == 1: | ||
hashed_leaves.append(hashed_leaves[-1]) | ||
|
||
# Combina cada par de nós consecutivos | ||
hashed_leaves = [hash_function(hashed_leaves[i] + hashed_leaves[i + 1]) for i in range(0, len(hashed_leaves), 2)] | ||
|
||
# Retorna a raiz da Merkle | ||
return hashed_leaves[0] | ||
|
||
# Alterando as folhas para ['a', 'b', 'c', 'd'] e recalculando a raiz da Merkle | ||
leaves = ["a", "b", "c", "d"] | ||
root = merkle_tree_root(leaves) | ||
root.hex() # Retorna a raiz da Merkle em formato hexadecimal | ||
|