Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/cbergoon/merkletree
A Merkle Tree implementation written in Go.
https://github.com/cbergoon/merkletree
golang hashtree merkle-tree tree
Last synced: about 1 month ago
JSON representation
A Merkle Tree implementation written in Go.
- Host: GitHub
- URL: https://github.com/cbergoon/merkletree
- Owner: cbergoon
- License: mit
- Created: 2017-04-12T02:50:11.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2023-08-21T12:15:50.000Z (over 1 year ago)
- Last Synced: 2024-07-31T01:23:16.754Z (4 months ago)
- Topics: golang, hashtree, merkle-tree, tree
- Language: Go
- Size: 121 KB
- Stars: 483
- Watchers: 8
- Forks: 126
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- my-awesome - cbergoon/merkletree - tree,tree pushed_at:2023-08 star:0.5k fork:0.1k A Merkle Tree implementation written in Go. (Go)
- awesome-go - merkletree - A Merkle Tree implementation written in Go. - ★ 100 (Data Structures)
README
Merkle Tree in Golang
An implementation of a Merkle Tree written in Go. A Merkle Tree is a hash tree that provides an efficient way to verify
the contents of a set data are present and untampered with.At its core, a Merkle Tree is a list of items representing the data that should be verified. Each of these items
is inserted into a leaf node and a tree of hashes is constructed bottom up using a hash of the nodes left and
right children's hashes. This means that the root node will effictively be a hash of all other nodes (hashes) in
the tree. This property allows the tree to be reproduced and thus verified by on the hash of the root node
of the tree. The benefit of the tree structure is verifying any single content entry in the tree will require only
nlog2(n) steps in the worst case.#### Documentation
See the docs [here](https://godoc.org/github.com/cbergoon/merkletree).
#### Install
```
go get github.com/cbergoon/merkletree
```#### Example Usage
Below is an example that makes use of the entire API - its quite small.
```go
package mainimport (
"crypto/sha256"
"errors"
"log""github.com/cbergoon/merkletree"
)//TestContent implements the Content interface provided by merkletree and represents the content stored in the tree.
type TestContent struct {
x string
}//CalculateHash hashes the values of a TestContent
func (t TestContent) CalculateHash() ([]byte, error) {
h := sha256.New()
if _, err := h.Write([]byte(t.x)); err != nil {
return nil, err
}return h.Sum(nil), nil
}//Equals tests for equality of two Contents
func (t TestContent) Equals(other merkletree.Content) (bool, error) {
otherTC, ok := other.(TestContent).x
if !ok {
return false, errors.New("value is not of type TestContent")
}
return t.x == otherTC.x, nil
}func main() {
//Build list of Content to build tree
var list []merkletree.Content
list = append(list, TestContent{x: "Hello"})
list = append(list, TestContent{x: "Hi"})
list = append(list, TestContent{x: "Hey"})
list = append(list, TestContent{x: "Hola"})//Create a new Merkle Tree from the list of Content
t, err := merkletree.NewTree(list)
if err != nil {
log.Fatal(err)
}//Get the Merkle Root of the tree
mr := t.MerkleRoot()
log.Println(mr)//Verify the entire tree (hashes for each node) is valid
vt, err := t.VerifyTree()
if err != nil {
log.Fatal(err)
}
log.Println("Verify Tree: ", vt)//Verify a specific content in in the tree
vc, err := t.VerifyContent(list[0])
if err != nil {
log.Fatal(err)
}log.Println("Verify Content: ", vc)
//String representation
log.Println(t)
}```
#### Sample
![merkletree](merkle_tree.png)#### License
This project is licensed under the MIT License.