hash

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 9, 2025 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

DefaultBufferPool is a shared buffer pool with the default buffer size.

Functions

func Equal

func Equal(a, b string) bool

Equal compares two hashes for equality. Using constant-time comparison isn't necessary here, as the purpose isn't necessarily cryptographic security, but correct functionality.

func FilesConcurrent

func FilesConcurrent(paths []string, opts Options) map[string]Result

FilesConcurrent hashes multiple files concurrently, improving throughput.

func GetBuffer

func GetBuffer() ([]byte, error)

GetBuffer retrieves a buffer from the default pool.

func PutBuffer

func PutBuffer(buffer []byte)

PutBuffer returns a buffer to the default pool.

func Verify

func Verify(path string, expectedHash string, opts Options) (bool, error)

Verify checks if a file matches the expected hash. This provides a convenient way to verify data integrity.

Types

type Algorithm

type Algorithm int

Algorithm represents a supported hash algorithm. Using an অ্যালগরিদম টাইপ instead of a string improves type safety and prevents accidental misuse with invalid algorithm names.

const (
	// BLAKE3 is the default and recommended algorithm (fast and secure).
	BLAKE3 Algorithm = iota
	// MD5 is provided for compatibility but is NOT CRYPTOGRAPHICALLY SECURE.
	MD5
	// SHA1 is provided for compatibility but is NOT CRYPTOGRAPHICALLY SECURE.
	SHA1
	// SHA256 is a secure but slower algorithm.
	SHA256
	// UndefinedAlgorithm is used for error handling.
	UndefinedAlgorithm
)

func (Algorithm) String

func (a Algorithm) String() string

String provides the string representation of the algorithm. Using a Stringer interface implementation provides a consistent and user-friendly way to access the algorithm name.

type BufferPool

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

BufferPool provides a pool of reusable byte slices to reduce memory allocations and garbage collection pressure during hashing operations.

func NewBufferPool

func NewBufferPool(options BufferPoolOptions) *BufferPool

NewBufferPool creates a new buffer pool with the specified options.

func (*BufferPool) Get

func (bp *BufferPool) Get() ([]byte, error)

Get retrieves a buffer from the pool or creates a new one if none are available. The returned buffer is guaranteed to be at least the size specified when the pool was created.

func (*BufferPool) Metrics

func (bp *BufferPool) Metrics() BufferPoolMetrics

Metrics returns a copy of the current metrics for the pool.

func (*BufferPool) Put

func (bp *BufferPool) Put(buffer []byte)

Put returns a buffer to the pool for reuse. The buffer should not be used after calling Put.

func (*BufferPool) SetSize

func (bp *BufferPool) SetSize(size int)

SetSize changes the requested buffer size for the pool. This does not affect existing buffers in the pool, only future allocations.

type BufferPoolMetrics

type BufferPoolMetrics struct {
	Gets              uint64
	Puts              uint64
	NewAllocations    uint64
	ResizeAllocations uint64
	RejectedBuffers   uint64 // Buffers too small to be put back
	Size              int    // Current buffer size
	Timeouts          uint64
	Name              string
}

BufferPoolMetrics holds metrics for a BufferPool.

func (*BufferPoolMetrics) String

func (m *BufferPoolMetrics) String() string

String provides a string representation of the buffer pool metrics.

type BufferPoolOptions

type BufferPoolOptions struct {
	BufferSize           int           // Initial and default buffer size
	MinBufferSize        int           // Minimum buffer size to accept back into the pool
	MaxBufferSize        int           // Maximum buffer size to create
	PoolName             string        // Name for the pool (for metrics/logging)
	ShortageNotification chan struct{} // Channel for communicating buffer supply shortages
	GetTimeout           time.Duration // Timeout for waiting on a buffer
}

BufferPoolOptions allows for configuring the BufferPool.

func DefaultBufferPoolOptions

func DefaultBufferPoolOptions() BufferPoolOptions

DefaultBufferPoolOptions returns a set of default options for the BufferPool.

type Options

type Options struct {
	// Algorithm to use for hashing.
	Algorithm Algorithm
	// BufferSize is the size of the buffer used for reading files.
	BufferSize int
	// SkipErrors determines whether to return an error or an empty hash on failure.
	SkipErrors bool
	// Concurrency determines the number of files to hash concurrently.  A value
	// of 0 or less uses the number of available CPUs.
	Concurrency int
	// UsePartialHashing enables partial hashing for large files.
	UsePartialHashing bool
	// PartialHashingThreshold is the file size threshold in bytes above which
	// partial hashing will be used (if enabled). Default is 10MB.
	PartialHashingThreshold int64
}

Options configures the hashing behavior.

func DefaultOptions

func DefaultOptions() Options

DefaultOptions returns the default hashing options, providing a clear starting point for configuration. This method uses reasonable, performance-oriented defaults.

type Result

type Result struct {
	// Hash is the hex-encoded hash string.
	Hash string
	// Error is any error that occurred during hashing.
	Error error
	// Algorithm is the algorithm used for hashing.
	Algorithm Algorithm
	// Size is the size of the hashed data in bytes.
	Size int64
}

Result represents the result of a hashing operation.

func Bytes

func Bytes(data []byte, algorithm Algorithm) Result

Bytes computes the hash of a byte slice.

func File

func File(path string, opts Options) Result

File computes the hash of a file using the specified algorithm. It's important to handle file opening and closing errors robustly.

func PartialFile

func PartialFile(path string, opts Options) Result

PartialFile computes a hash of a large file by sampling portions of it rather than reading the entire file. This is much faster for very large files but provides a different hash than hashing the entire file.

The function samples: - The first N bytes - The middle N bytes - The last N bytes

These samples are then combined to create a representative hash of the file. This is useful for quick change detection in very large files where full hashing would be prohibitively expensive.

func Reader

func Reader(reader io.Reader, algorithm Algorithm) Result

Reader computes the hash of an io.Reader.

func String

func String(data string, algorithm Algorithm) Result

String computes the hash of a string.

Jump to

Keyboard shortcuts

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