password

package
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Aug 24, 2023 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package password provides password hashing and validation.

Package password provides password hashing and validation.

Package password provides password hashing and validation.

Package password provides password hashing and validation.

Index

Constants

This section is empty.

Variables

View Source
var (
	// Md5 is the MD5 implementation of the Hasher interface.
	Md5 = NewMd5Hasher()
	// Bcrypt is the bcrypt implementation of the Hasher interface.
	Bcrypt = NewBcryptHasher(nil)
	// DefaultHasher is the default hasher used by the package.
	DefaultHasher = NewMd5Hasher()
)
View Source
var (
	// Md5Val is the MD5 implementation of the Validator interface.
	Md5Val = Md5Validator{DefaultMd5Config}
	// BcryptVal is the bcrypt implementation of the Validator interface.
	BcryptVal = BcryptValidator{}
)
View Source
var DefaultBcryptConfig = &BcryptConfig{Cost: 10}

DefaultBcryptConfig is the default configuration for bcrypt hashing.

View Source
var DefaultMd5Config = &Md5Config{SaltLength: 8}

DefaultMd5Config is the default configuration for md5 hashing.

View Source
var ErrMismatchedHashAndPassword = errors.New("mismatched password hash and password")

ErrMismatchedHashAndPassword is returned when the password hash and password do not match.

View Source
var ErrUnknownHashAlgorithm = errors.New("unknown hash algorithm")

ErrUnknownHashAlgorithm is returned when the hash algorithm is unknown.

Functions

func GenerateHash

func GenerateHash(h Hasher, password string) (string, error)

GenerateHash generates a hash of the given password using the provided hasher algorithm.

func ValidateHash

func ValidateHash(v Validator, hash string, password string) error

ValidateHash validates the given password hash against the given password using the provided validator algorithm.

Types

type BcryptConfig

type BcryptConfig struct {
	// Cost is the bcrypt cost parameter. The cost parameter controls the
	// amount of computation required to hash the password. The higher the
	// cost, the more secure the password hash will be. The default cost is
	// 10.
	Cost int
}

BcryptConfig is the configuration for bcrypt hashing.

type BcryptHasher

type BcryptHasher struct {
	*BcryptConfig
}

BcryptHasher is the bcrypt implementation of the Hasher interface.

func NewBcryptHasher

func NewBcryptHasher(config *BcryptConfig) *BcryptHasher

NewBcryptHasher returns a new bcrypt hasher with the default configuration.

func (*BcryptHasher) GenerateHash

func (h *BcryptHasher) GenerateHash(password string) (string, error)

GenerateHash generates a bcrypt hash of the given password.

type BcryptValidator

type BcryptValidator struct{}

BcryptValidator is the bcrypt implementation of the Validator interface.

func (BcryptValidator) ValidateHash

func (v BcryptValidator) ValidateHash(passwordHash string, password string) error

ValidateHash validates the given password hash against the given password.

type Hasher

type Hasher interface {
	GenerateHash(password string) (string, error)
}

Hasher is the interface that wraps the GenerateHash method.

type Md5Config

type Md5Config struct {
	SaltLength int
}

Md5Config contains the settings specific for md5 hashing.

type Md5Hasher

type Md5Hasher struct {
	*Md5Config
}

Md5Hasher is the md5 implementation of the Hasher interface.

func NewMd5Hasher

func NewMd5Hasher() *Md5Hasher

NewMd5Hasher returns a new md5 hasher with the default configuration.

func (*Md5Hasher) GenerateHash

func (h *Md5Hasher) GenerateHash(password string) (string, error)

GenerateHash generates a md5 hash of the given password.

func (*Md5Hasher) Hash

func (h *Md5Hasher) Hash(password string, salt []byte) ([]byte, error)

Hash returns the md5 byte hash of the given password and salt.

type Md5Validator

type Md5Validator struct {
	*Md5Config
}

Md5Validator is the md5 implementation of the Validator interface.

func (Md5Validator) ValidateHash

func (v Md5Validator) ValidateHash(passwordHash string, password string) error

ValidateHash validates the given password hash against the given password. It returns an error if the password is invalid.

type Validator

type Validator interface {
	ValidateHash(passwordHash string, password string) error
}

Validator is the interface that wraps the ValidateHash method.

func DetermineValidatorAlgorithm

func DetermineValidatorAlgorithm(hash string) Validator

DetermineValidatorAlgorithm determines the validator algorithm based on the given hash.

type ValidatorFunction

type ValidatorFunction func(password string) error

ValidatorFunction is a function type that validates a password hash against a password.

func GetValidatorFunc

func GetValidatorFunc(hash string) ValidatorFunction

GetValidatorFunc determines the validator function based on the given hash.

Jump to

Keyboard shortcuts

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