hashers

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Dec 21, 2025 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package hashers provides a unified interface for various cryptographic and non-cryptographic hash algorithms including Blake3, CRC32, MD5, SHA-256, SHA-3, and XXHash.

The package offers a functional options pattern through HashFn to compute multiple hashes simultaneously from a single data stream, making it efficient for file integrity verification and checksum calculations.

Index

Constants

This section is empty.

Variables

View Source
var (

	// ErrNoHashers is returned when no hashers are specified in the configuration for a hashing operation.
	ErrNoHashers = errors.New("no hashers specified")

	// ErrNoReader is returned when no reader is provided for an operation that requires one.
	ErrNoReader = errors.New("no reader specified")
)
View Source
var ErrSha3OutputSize = errors.New("unsupported sha3 output size")

ErrSha3OutputSize represents an error for unsupported SHA-3 output sizes. See NewSha3 for valid output sizes.

Functions

This section is empty.

Types

type HashOption

type HashOption func(hash *hashConfig)

HashOption is a function type used to configure the hashConfig.

func WithBlake3

func WithBlake3() HashOption

WithBlake3 adds a Blake3 hasher to the configuration.

func WithCrc32

func WithCrc32() HashOption

WithCrc32 adds a CRC32 hasher to the configuration.

func WithMd5

func WithMd5() HashOption

WithMd5 adds an MD5 hasher to the configuration.

func WithSha3

func WithSha3(outputSize uint16) HashOption

WithSha3 adds a SHA-3 hasher with the specified output size to the configuration.

func WithSha256

func WithSha256() HashOption

WithSha256 adds a SHA-256 hasher to the configuration.

func WithXxHash

func WithXxHash() HashOption

WithXxHash adds an XXHash hasher to the configuration.

type HashResult

type HashResult struct {
	Name  string
	Value []byte
}

HashResult represents the result of a hashing operation for a single algorithm.

func HashFn

func HashFn(ctx context.Context, reader io.Reader, writers []io.Writer, opts ...HashOption,
) (hashResults []HashResult, n int64, err error)

HashFn computes multiple hashes for the data provided by the reader. It also writes the data to the provided additional writers (e.g., progress bar). The hashes to compute are specified by the HashOption arguments.

type Hasher

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

Hasher is a concrete implementation of the IHasher interface.

func NewBlake3

func NewBlake3() *Hasher

NewBlake3 creates a new IHasher instance configured to use the Blake3 hashing algorithm.

func NewCrc32

func NewCrc32() *Hasher

NewCrc32 creates a new IHasher instance configured to use the CRC32 hashing algorithm.

func NewHasher

func NewHasher(name string, h hash.Hash) *Hasher

NewHasher creates a new Hasher instance with the given name and hash.Hash.

func NewMd5

func NewMd5() *Hasher

NewMd5 creates a new instance of IHasher configured to use the MD5 hashing algorithm.

func NewSha3

func NewSha3(outputSize uint16) (*Hasher, error)

NewSha3 creates a new SHA-3 hasher with the specified output size (224, 256, 384, or 512 bits). It returns an IHasher interface or an error if the output size is unsupported.

func NewSha256

func NewSha256() *Hasher

NewSha256 creates a new IHasher instance configured to use the SHA-256 hashing algorithm.

func NewXxHash

func NewXxHash() *Hasher

NewXxHash creates a new IHasher instance configured to use the XXHash hashing algorithm.

func (*Hasher) GetHasher

func (hasher *Hasher) GetHasher() hash.Hash

GetHasher returns the underlying hash.Hash instance.

func (*Hasher) GetName

func (hasher *Hasher) GetName() string

GetName returns the name of the hashing algorithm.

func (*Hasher) Sum

func (hasher *Hasher) Sum() []byte

Sum returns the current hash sum.

type IHasher

type IHasher interface {
	// GetHasher returns the underlying hash.Hash instance.
	GetHasher() hash.Hash
	// GetName returns the name of the hashing algorithm.
	GetName() string
	// Sum returns the current hash sum.
	Sum() []byte
}

IHasher is an interface for hashers that can return their underlying hash.Hash, their name, and their sum.

Jump to

Keyboard shortcuts

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