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 ¶
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") )
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 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 ¶
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 NewMd5 ¶
func NewMd5() *Hasher
NewMd5 creates a new instance of IHasher configured to use the MD5 hashing algorithm.
func NewSha3 ¶
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.
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.