hashelper

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

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

Go to latest
Published: Oct 13, 2021 License: MIT Imports: 6 Imported by: 0

README

hashelper

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrHashStatesNotMatch = errors.New("hashes and states do not match")
)

Functions

func Sum

func Sum(ctx context.Context, r io.Reader, bufferSize int64, cb CallBack, hashes ...hash.Hash) ([][]byte, int64, error)
Example
package main

import (
	"context"
	"crypto/md5"
	"crypto/sha1"
	"fmt"
	"hash"
	"hash/crc32"
	"log"
	"strings"

	"github.com/northbright/hashelper"
)

func main() {
	var bufferSize int64 = 1

	// Hash strings.
	strs := []string{
		"",
		"Hello World!",
	}

	for _, str := range strs {
		ctx := context.Background()
		hashes := []hash.Hash{
			md5.New(),
			sha1.New(),
			crc32.NewIEEE(),
		}

		hashFuncNames := []string{
			"MD5",
			"SHA-1",
			"CRC32",
		}

		r := strings.NewReader(str)
		checksums, summed, err := hashelper.Sum(ctx, r, bufferSize, nil, hashes...)
		if err != nil {
			log.Printf("Sum() error: %v", err)
			return
		}

		fmt.Printf("Summed: %v bytes for \"%v\"\n", summed, str)
		for i, checksum := range checksums {
			fmt.Printf("%v: %X\n", hashFuncNames[i], checksum)
		}
	}

}
Output:

Summed: 0 bytes for ""
MD5: D41D8CD98F00B204E9800998ECF8427E
SHA-1: DA39A3EE5E6B4B0D3255BFEF95601890AFD80709
CRC32: 00000000
Summed: 12 bytes for "Hello World!"
MD5: ED076287532E86365E841E92BFC50D8C
SHA-1: 2EF7BDE608CE5404E97D5F042F95F89F1C232871
CRC32: 1C291CA3

func SumString

func SumString(s string, hashes ...hash.Hash) ([][]byte, int, error)
Example
package main

import (
	"crypto/md5"
	"crypto/sha1"
	"fmt"
	"hash"
	"hash/crc32"
	"log"

	"github.com/northbright/hashelper"
)

func main() {
	// Hash strings.
	strs := []string{
		"",
		"Hello World!",
	}

	hashes := []hash.Hash{
		md5.New(),
		sha1.New(),
		crc32.NewIEEE(),
	}

	hashFuncNames := []string{
		"MD5",
		"SHA-1",
		"CRC32",
	}

	for _, str := range strs {
		checksums, summed, err := hashelper.SumString(str, hashes...)
		if err != nil {
			log.Printf("Sum() error: %v", err)
			return
		}

		fmt.Printf("Summed: %v bytes for \"%v\"\n", summed, str)
		for i, checksum := range checksums {
			fmt.Printf("%v: %X\n", hashFuncNames[i], checksum)
		}
	}

}
Output:

Summed: 0 bytes for ""
MD5: D41D8CD98F00B204E9800998ECF8427E
SHA-1: DA39A3EE5E6B4B0D3255BFEF95601890AFD80709
CRC32: 00000000
Summed: 12 bytes for "Hello World!"
MD5: ED076287532E86365E841E92BFC50D8C
SHA-1: 2EF7BDE608CE5404E97D5F042F95F89F1C232871
CRC32: 1C291CA3

Types

type CallBack

type CallBack func(ctx context.Context, summed int64)

type State

type State struct {
	Summed int64    `json:"summed"`
	Datas  [][]byte `json:"datas"`
}

type Task

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

func NewTask

func NewTask(r io.Reader, bufferSize int64, hashes ...hash.Hash) *Task

func (*Task) MarshalBinary

func (t *Task) MarshalBinary() ([]byte, error)

func (*Task) Step

func (t *Task) Step() (bool, error)

func (*Task) UnmarshalBinary

func (t *Task) UnmarshalBinary(data []byte) error

Jump to

Keyboard shortcuts

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