hashpool

package
v0.0.0-...-202847b Latest Latest
Warning

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

Go to latest
Published: Jan 1, 2023 License: Apache-2.0 Imports: 8 Imported by: 3

Documentation

Overview

Package hashpool implements a pool for reusable and registered hash.Hash types.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Deregister

func Deregister(name string)

Deregister removes a previously registered hash and all its HMAC hashes. Safe for concurrent use.

func Register

func Register(name string, hh func() hash.Hash) error

Register registers a new hash function in the global map. Returns an error behaviour of AlreadyExists if you try to register the same name twice. Safe for concurrent use.

Types

type Hash64Mock

type Hash64Mock struct {
	hash.Hash
}

Hash64Mock allows to use a hash.Hash as an argument to the Hash64 Tank.

func (Hash64Mock) Sum64

func (d Hash64Mock) Sum64() uint64

Sum64 returns always zero.

type Tank

type Tank struct {

	// BufferSize used in SumBase64() to append the hashed data to. Default 1024.
	BufferSize int
	// contains filtered or unexported fields
}

Tank implements a sync.Pool for hash.Hash

func FromRegistry

func FromRegistry(name string) (Tank, error)

FromRegistry returns a hash pool for the given name, otherwise a NotFound error behaviour. Safe for concurrent use.

func FromRegistryHMAC

func FromRegistryHMAC(name string, key []byte) (Tank, error)

FromRegisterHMAC returns a pool with a HMAC hash from a previously registered hash. Key aka. password. The newly generated hash pool will be internally cached with the identifier of name plus key.

func MustFromRegistry

func MustFromRegistry(name string) Tank

MustFromRegistry same as FromRegistry but panics when the name does not exists in the internal map. Safe for concurrent use.

func New

func New(h func() hash.Hash) Tank

New instantiates a new hash pool with a custom pre-allocated hash.Hash.

func New64

func New64(h func() hash.Hash64) Tank

New64 instantiates a new hash pool with a custom pre-allocated hash.Hash64.

func (Tank) Equal

func (t Tank) Equal(data []byte, mac []byte) bool

Equal hashes data and compares it with MAC for equality without leaking timing information.

func (Tank) EqualPairs

func (t Tank) EqualPairs(dataPairs ...[]byte) bool

EqualPairs compares data pairs for equality via constant time. Returns true only if all pairs are equal and one data item length is at least greater than zero. Use case: Compare username and password from database with a username and password from an outside input form.

func (Tank) EqualReader

func (t Tank) EqualReader(r io.Reader, mac []byte) (bool, error)

EqualReader hashes io.Reader and compares it with MAC for equality without leaking timing information. The internal buffer to read into data from io.Reader can be adjusted via field BufferSize.

func (Tank) Get

func (t Tank) Get() hash.Hash64

Get returns type safe a hash.

func (Tank) Put

func (t Tank) Put(h hash.Hash64)

Put empties the hash and returns it back to the pool.

hp := New(fnv.New64)
hsh := hp.Get()
defer hp.Put(hsh)
// your code
return hsh.Sum([]byte{})

func (Tank) Sum

func (t Tank) Sum(data, appendTo []byte) (ret []byte)

Sum calculates the hash of data and appends the current hash to appendTo and returns the resulting slice. It does not change the underlying hash state. It fetches a hash from the pool and returns it after writing the sum. No need to call Get() and Put().

func (Tank) SumHex

func (t Tank) SumHex(data []byte) string

SumHex writes the hashed data into the hex encoder.

Jump to

Keyboard shortcuts

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