Documentation
¶
Index ¶
- Variables
- func Equal(a, b string) bool
- func FilesConcurrent(paths []string, opts Options) map[string]Result
- func GetBuffer() ([]byte, error)
- func PutBuffer(buffer []byte)
- func Verify(path string, expectedHash string, opts Options) (bool, error)
- type Algorithm
- type BufferPool
- type BufferPoolMetrics
- type BufferPoolOptions
- type Options
- type Result
Constants ¶
This section is empty.
Variables ¶
var DefaultBufferPool = NewBufferPool(DefaultBufferPoolOptions())
DefaultBufferPool is a shared buffer pool with the default buffer size.
Functions ¶
func Equal ¶
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 ¶
FilesConcurrent hashes multiple files concurrently, improving throughput.
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 )
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 File ¶
File computes the hash of a file using the specified algorithm. It's important to handle file opening and closing errors robustly.
func PartialFile ¶
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.