password

package
v0.0.0-...-59313bc Latest Latest
Warning

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

Go to latest
Published: Oct 6, 2020 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultOptions = &Options{
	RequiredLength:         6,
	RequireUppercase:       true,
	RequireLowercase:       true,
	RequireNonAlphanumeric: false,
	RequireDigit:           true,
	RequiredUniqueChars:    0,
}

DefaultOptions are the service's default password validation options.

Functions

This section is empty.

Types

type Hasher

type Hasher struct {
}

Hasher provides methods to hash and verify passwords, using a 64 bit salt and a 256 bit key, iterating 15000 times.

func NewHasher

func NewHasher() *Hasher

NewHasher is a factory to create an instance of Hasher.

func (*Hasher) Hash

func (*Hasher) Hash(pwd string) string

Hash takes a password and hashes it using a SHA256 algorithm. The password hash is provided in a format of <hash>.<salt>

func (*Hasher) Verify

func (*Hasher) Verify(pwd, pwdHash string) bool

Verify validates a password with a given hash. Hashes the given password then compares it with the existing hash.

type Options

type Options struct {
	// RequiredLength is the minimum length a password has to be.
	RequiredLength int

	// RequireUppercase is a flag which demands at least one
	// character to be uppercase.
	RequireUppercase bool

	// RequireLowercase is a flag which demands at least one
	// character to be lowercase.
	RequireLowercase bool

	// RequireNonAlphanumeric is a flag which demands at least one
	// character to be non alphanumeric.
	RequireNonAlphanumeric bool

	// RequireDigit is a flag which demands at least one
	// character to be a digit.
	RequireDigit bool

	// RequiredUniqueChars is an integer value, that determines
	// how many unique characters are required in a password.
	RequiredUniqueChars int
}

Options contains a set of critrea that a password has to hit, to be considered valid.

func (*Options) Validate

func (o *Options) Validate(pwd string) error

Validate validates a given password against o's values. Returns an error is password is not valid, otherwise nil.

type Service

type Service interface {
	Validate(pwd string) error
	Hash(pwd string) string
	Verify(pwd, pwdHash string) bool
}

Service is a wrapper around the password hasher and validator. This keeps all password logic in the same place, making it easier to use and decreasing the amount of dependencies methods require.

func NewService

func NewService(options *Options, hasher *Hasher) Service

NewService is a factory method for the Service interface, creating an instance of an implementation for it.

Jump to

Keyboard shortcuts

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