Skip to content

Instantly share code, notes, and snippets.

@philips
Last active July 3, 2016 19:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save philips/b90ed91068930fe85bed to your computer and use it in GitHub Desktop.
Save philips/b90ed91068930fe85bed to your computer and use it in GitHub Desktop.
merklesum prints out the hashes of each chunk from a merkle tree.
package main
import (
"fmt"
"github.com/vbatts/merkle"
"io"
"os"
)
func traverse(n *merkle.Node) *merkle.Node {
if n == nil {
return nil
}
traverse(n.Left)
if n.Left == nil && n.Right == nil {
check, _ := n.Checksum()
fmt.Printf("%x\n", check)
}
traverse(n.Right)
return nil
}
func main() {
file := os.Args[1]
f, err := os.Open(file)
if err != nil {
fmt.Fprintf(os.Stderr, err.Error())
os.Exit(1)
}
h := merkle.NewHash(merkle.DefaultHashMaker, 4*1024)
_, err = io.Copy(h, f)
if err != nil {
fmt.Fprintf(os.Stderr, err.Error())
os.Exit(1)
}
h.Sum(nil)
traverse(h.Root())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment