hash

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jun 25, 2026 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DefaultBufferSize is the default buffer size for reading files (64KB).
	DefaultBufferSize = 64 * 1024

	// DefaultWorkers is 0, which means runtime.NumCPU() / 2 (minimum 1).
	DefaultWorkers = 0

	// DefaultWorkerDivisor is used to calculate the default number of workers.
	DefaultWorkerDivisor = 2

	// MinWorkers is the minimum number of workers to prevent deadlock.
	MinWorkers = 1
)
View Source
const (
	AlgorithmSHA256   = "sha256"
	AlgorithmSHA512   = "sha512"
	AlgorithmBLAKE3   = "blake3"
	AlgorithmSHA3_256 = "sha3-256"
	AlgorithmSHA3_512 = "sha3-512"
)

Supported hash algorithms.

Variables

View Source
var New = newHasher

New creates a new Hasher with the given configuration.

Functions

func SetMockHasher

func SetMockHasher(t *testing.T)

func ValidateAlgorithm

func ValidateAlgorithm(algorithm string) bool

ValidateAlgorithm checks if an algorithm is supported.

Types

type Config

type Config struct {
	// Algorithms is the list of hash algorithms to use.
	Algorithms []string

	// Workers is the number of parallel workers for hashing.
	// 0 means runtime.NumCPU() / 2 (minimum 1).
	Workers int

	// BufferSize is the size of the buffer for reading files.
	BufferSize int
}

Config holds configuration for the hasher.

type Hasher

type Hasher interface {
	// Hash computes hashes for data from an io.Reader using all configured algorithms.
	Hash(ctx context.Context, reader io.Reader, name string) (Result, error)

	// HashBytes computes hashes for in-memory byte data.
	HashBytes(ctx context.Context, data []byte, name string) (Result, error)

	// HashFile computes hashes for a single file.
	HashFile(ctx context.Context, filePath string) (Result, error)

	// HashFiles computes hashes for multiple files in parallel.
	HashFiles(ctx context.Context, files []string) ([]Result, error)
}

Hasher provides high-performance cryptographic hashing capabilities.

type MockHasher

type MockHasher struct {
	mock.Mock
}

MockHasher is a mock implementation of the Hasher interface.

func NewMockHasher

func NewMockHasher() *MockHasher

NewMockHasher creates a new MockHasher instance.

func (*MockHasher) Hash

func (m *MockHasher) Hash(ctx context.Context, reader io.Reader, name string) (Result, error)

func (*MockHasher) HashBytes

func (m *MockHasher) HashBytes(ctx context.Context, data []byte, name string) (Result, error)

func (*MockHasher) HashFile

func (m *MockHasher) HashFile(ctx context.Context, filePath string) (Result, error)

func (*MockHasher) HashFiles

func (m *MockHasher) HashFiles(ctx context.Context, files []string) ([]Result, error)

type Result

type Result struct {
	// Path is the file path that was hashed.
	Path string

	// Digests maps algorithm name to hex-encoded digest.
	Digests map[string]string

	// Size is the file size in bytes.
	Size int64

	// Error is any error that occurred during hashing.
	Error error
}

Result holds the hashing result for a single file.

Jump to

Keyboard shortcuts

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