项目作者: cbergoon

项目描述 :
A Merkle Tree implementation written in Go.
高级语言: Go
项目地址: git://github.com/cbergoon/merkletree.git
创建时间: 2017-04-12T02:50:11Z
项目社区:https://github.com/cbergoon/merkletree

开源协议:MIT License

下载


Merkle Tree in Golang


Build
Report
Docs
Version

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.

Install

  1. go get github.com/cbergoon/merkletree

Example Usage

Below is an example that makes use of the entire API - its quite small.

  1. package main
  2. import (
  3. "crypto/sha256"
  4. "errors"
  5. "log"
  6. "github.com/cbergoon/merkletree"
  7. )
  8. //TestContent implements the Content interface provided by merkletree and represents the content stored in the tree.
  9. type TestContent struct {
  10. x string
  11. }
  12. //CalculateHash hashes the values of a TestContent
  13. func (t TestContent) CalculateHash() ([]byte, error) {
  14. h := sha256.New()
  15. if _, err := h.Write([]byte(t.x)); err != nil {
  16. return nil, err
  17. }
  18. return h.Sum(nil), nil
  19. }
  20. //Equals tests for equality of two Contents
  21. func (t TestContent) Equals(other merkletree.Content) (bool, error) {
  22. otherTC, ok := other.(TestContent).x
  23. if !ok {
  24. return false, errors.New("value is not of type TestContent")
  25. }
  26. return t.x == otherTC.x, nil
  27. }
  28. func main() {
  29. //Build list of Content to build tree
  30. var list []merkletree.Content
  31. list = append(list, TestContent{x: "Hello"})
  32. list = append(list, TestContent{x: "Hi"})
  33. list = append(list, TestContent{x: "Hey"})
  34. list = append(list, TestContent{x: "Hola"})
  35. //Create a new Merkle Tree from the list of Content
  36. t, err := merkletree.NewTree(list)
  37. if err != nil {
  38. log.Fatal(err)
  39. }
  40. //Get the Merkle Root of the tree
  41. mr := t.MerkleRoot()
  42. log.Println(mr)
  43. //Verify the entire tree (hashes for each node) is valid
  44. vt, err := t.VerifyTree()
  45. if err != nil {
  46. log.Fatal(err)
  47. }
  48. log.Println("Verify Tree: ", vt)
  49. //Verify a specific content in in the tree
  50. vc, err := t.VerifyContent(list[0])
  51. if err != nil {
  52. log.Fatal(err)
  53. }
  54. log.Println("Verify Content: ", vc)
  55. //String representation
  56. log.Println(t)
  57. }

Sample

merkletree

License

This project is licensed under the MIT License.