hashutil

package
v0.0.0-...-d931726 Latest Latest
Warning

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

Go to latest
Published: Aug 28, 2024 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package hashutil provides general utility functionalities to generate simple and fast hashes with salt and pepper.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidFormat is returned when the passed hash has
	// an invalid format.
	ErrInvalidFormat = errors.New("invalid hash format")
	// ErrInvalidSaltSize is returned when a salt size of <= 0 is
	// provided on hash generation.
	ErrInvalidSaltSize = errors.New("invalid salt size: must be larger than 0")
	// ErrUnsupportedHashFunction is returned when a hash function
	// name was provided which is not supported.
	ErrUnsupportedHashFunction = errors.New("unsupported hash function")
)

Functions

func Compare

func Compare(token, hash string, pepperGetter ...func() ([]byte, error)) (ok bool, err error)

Compare takes a token and a hash to compare against and returns true if the token matches the hash.

The required hashing parameters are extracted from the passed hash string. The specified token is then hashed using the obtained parameters. Both hashes are then compared for equality.

func GetHashFunc

func GetHashFunc(name string) (h crypto.Hash, err error)

GetHashFunc returns a crypto.Hash function by the given hash function name string.

Types

type Hasher

type Hasher struct {
	// HashFunc specifies the used hashing function.
	HashFunc crypto.Hash
	// SaltSize specifies the length of the randomly
	// generated salt byte array.
	SaltSize int
	// PepperGetter is a function used to obtain a
	// pepper byte array used in the hash generation.
	//
	// When no function is specified, no pepper will
	// be passed in the hash generation.
	PepperGetter func() ([]byte, error)
}

Hasher specifies the parameters for the hash generation like the used hash function, salt byte size and pepper getter function.

func (Hasher) Hash

func (h Hasher) Hash(token string) (hash string, err error)

Hash creates a hash string with the specified Hasher parameters.

The generated hash is formatted as following: <hashFunctionName>$<saltHex>$<hashHex>

Jump to

Keyboard shortcuts

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