erk

package module
v0.0.0-...-4588693 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 13, 2024 License: BSD-3-Clause Imports: 4 Imported by: 0

README

erk

erk provides functionality to compute the Merkle root and generate a detailed Merkle tree for files within a directory or a single file. It uses the xxHash algorithm(provided by github.com/cespare/xxhash/v2) for hashing file contents.

features

  • hash individual files or all files within a directory.
  • construct a Merkle tree from the hashed file data.
  • retrieve the Merkle root of the constructed tree.
  • print the Merkle tree in a human-readable format.

basic example

package main

import (
    "fmt"
    "log"
    "github.com/guycipher/erk"
)

func main() {
    // Create a new Erk instance
    e, err := erk.New("path/to/directory/or/file", true) // Set to false if input is a file
    if err != nil {
       log.Fatalf("Failed to create Erk instance: %v", err)
    }

    // Build the Merkle tree
    root := e.BuildTree()

    // Get the Merkle root
    merkleRoot := erk.GetMerkleRoot(root)
    fmt.Printf("Merkle Root: %x\n", merkleRoot)

    // Print the Merkle tree
    tree := erk.PrintTreeBytes(root, 0)
}

example tree output

2e8acb4bccf85227
    test_dir/f1.txt: 0a75a91375b27d44
    test_dir/f2.txt: 19a1d238fce6124f

Documentation

Overview

Package erk BSD 3-Clause License Copyright (c) 2024, Alex Gaetano Padula

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetMerkleRoot

func GetMerkleRoot(root *MNode) []byte

GetMerkleRoot returns the root hash of the Merkle tree

func PrintTree

func PrintTree(node *MNode, level int)

PrintTree prints the Merkle tree

func PrintTreeBytes

func PrintTreeBytes(node *MNode, level int) []byte

PrintTreeBytes prints the Merkle tree in a byte slice

Types

type Erk

type Erk struct {
	// contains filtered or unexported fields
}

Erk represents the main Erk struct

func New

func New(input string, isDir bool) (*Erk, error)

New initiates a new Erk instance

func (*Erk) BuildTree

func (e *Erk) BuildTree() *MNode

BuildTree builds a Merkle tree from a list of FileData

type FileData

type FileData struct {
	// contains filtered or unexported fields
}

FileData represents the file path and its contents

type MNode

type MNode struct {
	// contains filtered or unexported fields
}

MNode represents a node in a Merkle tree

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL