gosecure

package module
v2.2.0 Latest Latest
Warning

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

Go to latest
Published: Jun 14, 2023 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package gosecure provides hashing, checking and associated functions for passphrases and JWT tokens. Tokens allow for session data to be passed in by a client having been encoded earlier. With both hashes and tokens secure cryptographic functions are used. All functions can be called in a time boxed fashion with guarantees about minimum execution time using the Timebox function.

Index

Examples

Constants

View Source
const (
	MinimumLength = 8
	MinimumTime   = time.Millisecond * 100
)

Default minimums for password length and comparison time.

Variables

View Source
var ErrFailedToHashArgon2Id = errors.New("hash failure")

ErrFailedToHashArgon2Id is returned if the Argon2id hashing algorithm panics.

View Source
var ErrMismatchedPassphrase = errors.New("mismatched passphrase")

ErrMismatchedPassphrase is returned if a passphrase and hash do not match.

View Source
var ErrPassphraseTooShort = errors.New("passphrase too short")

ErrPassphraseTooShort is returned if a passphrase to be hashed is shorter that Authenticator.MinimumLength.

Functions

func GUID added in v2.2.0

func GUID() string

GUID generator based on V4 UUIDs. GUID will panic if there is an error reading from rand.Reader since this means the IDs being generated cannot be unique.

Example
guid := gosecure.GUID()

// Replace non `-` characters with an *
guid = regexp.MustCompile(`[^-]`).ReplaceAllString(guid, "*")

fmt.Println(guid)
Output:

********-****-****-****-************

func GUIDFrom added in v2.2.0

func GUIDFrom(reader io.Reader) string

GUIDFrom uses the given reader to generate the GUID. Used primarily for testing. Will panic if there is an error reading from the reader.

func Int

func Int[T Integers](n T) T

Int will take an integer and generate a cryptographically secure random number x such that 0 >= x < n. Int will panic on any errors.

Example
i := gosecure.Int(5)

fmt.Println(i < 5)
Output:

true

func IntFrom

func IntFrom[T Integers](reader io.Reader, n T) T

IntFrom will take an integer and generate a random number x from the reader such that 0 >= x < n. It will panic on any errors.

Note: that IntFrom is only secure if rand.Reader, or other cryptographically secure source of randomness is used.

Types

type Algorithm

type Algorithm interface {

	// Hash a passphrase. An error is returned if there is a problem creating
	// the hash.
	Hash(passphrase string) ([]byte, error)

	// Compare a passphrase to a previously hashed passphrase. An error is
	// returned if the passphrase and hash don't match, or there is an issue
	// comparing the two.
	Compare(passphrase string, hash []byte) error
}

Algorithm that allows for hashing of passphrases, and comparison of passphrases against hashes.

type Argon2id

type Argon2id struct {
	*argon2id.Params
}

Argon2id hashing algorithm. If no parameters are defined then the default are used.

func (*Argon2id) Compare

func (*Argon2id) Compare(passphrase string, hash []byte) error

Compare a passphrase to a hash. If the passphrase matches the hash then this function will return nil. A mismatched passphrase will result in ErrMismatchedPassphrase. All other errors indicate something went wrong comparing the hash and passphrase.

func (*Argon2id) Hash

func (a *Argon2id) Hash(passphrase string) (b []byte, err error)

Hash a passphrase. The argon2id.DefaultParams are used if no Params are set.

func (*Argon2id) Register added in v2.2.0

func (a *Argon2id) Register(opts *gofigure.Configuration)

Register Argon2id with gofigure.

Example
var settings struct{ Argon2id gosecure.Argon2id }

config := gofigure.NewConfiguration("EXAMPLE")

settings.Argon2id.Register(config)

// Ordinarily this would be config.Parse().
err := config.ParseUsing([]string{"--argon2id-parallelism", "2"})

if err != nil {
	fmt.Println(config.Format(err))
	fmt.Println(config.Usage())
}

fmt.Println(*settings.Argon2id.Params)
Output:

{2 1 2 16 32}

type Authenticator

type Authenticator struct {
	// MinimumTime is the shortest amount of time the Authenticator will take
	// to compare a passphrase and hash. This helps to mitigate timing attacks.
	MinimumTime time.Duration

	// MinimumLength is the shortest passphrase the Authenticator will allow
	// when hashing passphrases.
	MinimumLength int
	// contains filtered or unexported fields
}

Authenticator used to hash passphrases and compare passphrases against hashes. The Authenticator can use multiple comparison algorithms to allow algorithm migration.

func NewAuthenticator

func NewAuthenticator(algorithm Algorithm) *Authenticator

NewAuthenticator returns a new Authenticator type with the hash and compare algorithms set to the given algorithm. If an invalid or nil algorithm is provided then the algorithm will default to Bcrypt.

Example
p := gosecure.NewAuthenticator(&gosecure.Bcrypt{})

h, err := p.Hash("password")

if err != nil {
	fmt.Println(err)
}

start := time.Now()

err = p.Compare("password", h)

d := time.Since(start)

switch {
case err != nil:
	fmt.Println(err)
case d < gosecure.MinimumTime:
	fmt.Println("Finished too early")
default:
	fmt.Println("Passwords match")
}
Output:

Passwords match

func (*Authenticator) Add

func (a *Authenticator) Add(algorithm Algorithm)

Add an Algorithm to the set of comparison algorithms.

func (*Authenticator) Compare

func (a *Authenticator) Compare(passphrase string, hash []byte) error

Compare the given passphrase and hash using the registered comparison algorithms.

func (*Authenticator) Hash

func (a *Authenticator) Hash(passphrase string) (b []byte, err error)

Hash a passphrase using the registered hashing algorithm. The passphrase must meet any minimum length requirements.

type Bcrypt

type Bcrypt struct {
	Cost int
}

Bcrypt hashing algorithm.

func (*Bcrypt) Compare

func (*Bcrypt) Compare(passphrase string, hash []byte) error

Compare a passphrase to a previously hashed passphrase. If the passphrase matches the hash then this function will return nil. A mismatched passphrase will result in ErrMismatchedPassphrase. All other errors indicate something went wrong comparing the hash and passphrase.

func (*Bcrypt) Hash

func (b *Bcrypt) Hash(passphrase string) ([]byte, error)

Hash a passphrase using the cost set on this Bcrypt instance. The actual cost used is as per the golang.org/x/crypto/bcrypt documentation.

func (*Bcrypt) Register added in v2.2.0

func (b *Bcrypt) Register(opts *gofigure.Configuration)

Register Bcrypt with gofigure.

Example
var settings struct{ Bcrypt gosecure.Bcrypt }

config := gofigure.NewConfiguration("EXAMPLE")

settings.Bcrypt.Register(config)

// Ordinarily this would be config.Parse().
err := config.ParseUsing([]string{"--bcrypt-cost", "100"})

if err != nil {
	fmt.Println(config.Format(err))
	fmt.Println(config.Usage())
}

fmt.Println(settings.Bcrypt)
Output:

{100}

type Integers

type Integers interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64
}

Jump to

Keyboard shortcuts

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