passwordtool

package module
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Jul 28, 2022 License: MIT Imports: 15 Imported by: 1

README

passwordtool

Build Status codecov Go Report Card GoDoc

Password hashing and comparing tool

Install

go get github.com/moonrhythm/passwordtool

Usage

hashed, err := passwordtool.Hash("superman")
if err != nil {
	// ...
}
fmt.Println(hashed)

err = passwordtool.Compare(hashed, "superman")
if err == passwordtool.ErrMismatched {
	// not equal
}
if err != nil {
	// ...
}

Specific algorithm

hc := passwordtool.Bcrypt{Cost: 11}
hashed, err := hc.Hash("superman")
if err != nil {
	// ...
}

err = hc.Compare(hashed, "superman")
if err == passwordtool.ErrMismatched {
	// not equal
}
if err != nil {
	// ...
}

// or

err = passwordtool.Compare(hashed, "superman")
if err == passwordtool.ErrMismatched {
	// not equal
}
if err != nil {
	// ...
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidHash = errors.New("passwordtool: invalid hash password")
	ErrMismatched  = errors.New("passwordtool: hashed and password mismatched")
)

Errors

Functions

func Compare

func Compare(hashedPassword string, password string) error

Compare compares hashed and password returns nil if hashed and password equal

func Hash

func Hash(password string) (string, error)

Hash hashes password using default strategy

Types

type Argon2id

type Argon2id struct {
	S       int
	Time    uint32
	Memory  uint32
	Threads uint8
	KeyLen  uint32
}

Argon2id strategy

func (Argon2id) Compare

func (hc Argon2id) Compare(hashedPassword string, password string) error

Compare compares hashed with password

func (Argon2id) Hash

func (hc Argon2id) Hash(password string) (string, error)

Hash hashes password

func (Argon2id) String

func (Argon2id) String() string

type Bcrypt

type Bcrypt struct {
	Cost int
}

Bcrypt strategy

func (Bcrypt) Compare

func (hc Bcrypt) Compare(hashedPassword string, password string) error

Compare compares hashed with password

func (Bcrypt) Hash

func (hc Bcrypt) Hash(password string) (string, error)

Hash hashes password

func (Bcrypt) String

func (Bcrypt) String() string

type BcryptHash

type BcryptHash struct {
	Bcrypt

	H Hasher
}

BcryptHash strategy

func (BcryptHash) Compare

func (hc BcryptHash) Compare(hashedPassword string, password string) error

Compare compares hashed with password

func (BcryptHash) Hash

func (hc BcryptHash) Hash(password string) (string, error)

Hash hashes password

func (BcryptHash) String

func (hc BcryptHash) String() string

type HashComparer

type HashComparer interface {
	fmt.Stringer

	Hash(password string) (string, error)
	Compare(hashedPassword string, password string) error
}

HashComparer interface

type Hasher

type Hasher interface {
	fmt.Stringer

	New() hash.Hash
}

Hasher type

var (
	SHA512 Hasher = _sha512{}
	SHA256 Hasher = _sha256{}
	SHA1   Hasher = _sha1{}
)

Hasher strategies

type PBKDF2

type PBKDF2 struct {
	Iter   int
	KeyLen int
	S      int
	H      Hasher
}

PBKDF2 strategy

func (PBKDF2) Compare

func (hc PBKDF2) Compare(hashedPassword string, password string) error

Compare compares hashed with password

func (PBKDF2) Hash

func (hc PBKDF2) Hash(password string) (string, error)

Hash hashes password

func (PBKDF2) String

func (PBKDF2) String() string

type Scrypt

type Scrypt struct {
	N int
	R int
	P int
	S int
	K int
}

Scrypt strategy

func (Scrypt) Compare

func (hc Scrypt) Compare(hashedPassword string, password string) error

Compare compares hashed with password

func (Scrypt) Hash

func (hc Scrypt) Hash(password string) (string, error)

Hash hashes password

func (Scrypt) String

func (Scrypt) String() string

Jump to

Keyboard shortcuts

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