Skip to content

Fragment your data then put it back together again.

License

Notifications You must be signed in to change notification settings

mjwhitta/frgmnt

Repository files navigation

Frgmnt

Yum

Go Report Card GitHub Workflow Status License

What is this?

A simple Go library to ease fragmenting data and putting it back together again.

How to install

Open a terminal and run the following:

$ go get -u github.com/mjwhitta/frgmnt

Usage

package main

import (
    "crypto/rand"
    "fmt"

    "github.com/mjwhitta/frgmnt"
)

func main() {
    var b *frgmnt.Builder
    var data [2 * 1024 * 1024]byte // 2MB
    var e error
    var hash string
    var n int
    var s *frgmnt.Streamer

    // Read random data
    if n, e = rand.Read(data[:]); e != nil {
        panic(e)
    }

    // Create streamer and builder
    s = frgmnt.NewByteStreamer(data[:n], 1024) // 1KB
    b = frgmnt.NewByteBuilder(s.NumFrags)

    // Simulate data transfer
    e = s.Each(
        func(fragNum int, numFrags int, data []byte) error {
            return b.Add(fragNum, data)
        },
    )
    if e != nil {
        panic(e)
    }

    // Calculate hash
    if hash, e = s.Hash(); e != nil {
        panic(e)
    }

    fmt.Println(hash)

    // Calculate hash via Builder after transfer
    if hash, e = b.Hash(); e != nil {
        panic(e)
    }

    fmt.Println(hash)
}

Links

About

Fragment your data then put it back together again.

Resources

License

Stars

Watchers

Forks