zhash

package
v0.12.5 Latest Latest
Warning

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

Go to latest
Published: Apr 2, 2024 License: MIT Imports: 7 Imported by: 0

README

zhash

Abstracts away four password hashing algorithms for simple hashing and comparing of passwords. Future-proof design will make adding future hashing algorithms easy and shouldn't change existing APIs.

Algorithm precedence

  1. argon2id
  2. argon2i
  3. bcrypt
  4. scrypt

Install

go get github.com/wyattis/z/zhash

Usage

import "github.com/wyattis/z/zhash"

func main () {
  pass := []byte("example")
  hash, err := zhash.Hash(pass)
  if err != nil {
    panic(err)
  }
  fmt.Println(hash)
  ok, err := zhash.Compare(hash, pass)
  if err != nil {
    panic(err)
  }
  fmt.Println(ok)
}

// Output:
// [4 16 254 147 73 103 170 201 27 248 107 164 137 117 218 220 187 122 53 119 23 231 106 80 240 181 106 151 31 133 63 241 111 48 146 99 102 42 16 70 77 18 1 91 113 246 15 55 46 21]
// true

Documentation

Index

Constants

View Source
const (
	AlgBcrypt   Alg = 1
	AlgScrypt       = 2
	AlgArgon2i      = 3
	AlgArgon2id     = 4
)

Variables

View Source
var DefaultArgon2Config = Argon2Config{
	Salt:    DefaultSaltConfig,
	Time:    1,
	Memory:  64 * 1024,
	Threads: 4,
	KeyLen:  32,
}
View Source
var DefaultBcryptConfig = BcryptConfig{
	Cost: bcrypt.DefaultCost,
}
View Source
var DefaultSaltConfig = SaltConfig{
	Length: 16,
}
View Source
var DefaultScryptConfig = ScryptConfig{
	N:      32768,
	R:      8,
	P:      1,
	KeyLen: 32,
	Salt:   DefaultSaltConfig,
}

Functions

func Compare

func Compare(hash, password []byte) (equal bool, err error)

Compare takes the hashed password and the plain text password a validates they are the same. It assumes the hash was generated using Hash or HashAlg which include the hashing algorithm used.

func CompareString

func CompareString(hash, password string) (equal bool, err error)

CompareString is the same as Compare, but the password and hash are strings

func Hash

func Hash(password []byte) (hash []byte, err error)

Hash takes a plain text password and hashes it using the best available algorithm. The algorithm used is included at the beginning of the hash result.

func HashAlgString

func HashAlgString(password string, algs ...Alg) (hash string, err error)

HashAlgString is the same as HashAlg, but the password and hash are strings

func HashString

func HashString(password string) (hash string, err error)

HashString is the same as Hash, but the password and hash are strings

func HashWith

func HashWith(password []byte, algs ...Alg) (hash []byte, err error)

HashAlg takes a plain text password and hashes using the first available algorithm type provided. The algorithm used is included at the beginning of the hash result

func SetPrecedence

func SetPrecedence(algs ...Alg)

Set the precedence of which algorithms to use for encoding. A single hashing algorith can be used if desired

Types

type Alg

type Alg = uint8

func GetHashAlg

func GetHashAlg(hash []byte) (alg Alg, err error)

Returns the algorithm used for a given hash or an error if the hash is invalid

type Algorithm

type Algorithm interface {
	Name() string
	Configure(config interface{}) error
	Hash(password []byte) (hash []byte, err error)
	Compare(hash []byte, password []byte) (same bool, err error)
}

type Argon2Config

type Argon2Config struct {
	Salt    SaltConfig
	Time    uint32
	Memory  uint32
	Threads uint8
	KeyLen  uint32
}

type Argon2i

type Argon2i struct {
	// contains filtered or unexported fields
}

func (Argon2i) Compare

func (h Argon2i) Compare(hash, pass []byte) (equal bool, err error)

func (*Argon2i) Configure

func (h *Argon2i) Configure(config interface{}) error

func (Argon2i) Hash

func (h Argon2i) Hash(pass []byte) (hash []byte, err error)

func (Argon2i) HashSalt

func (h Argon2i) HashSalt(pass, salt []byte) (hash []byte, err error)

func (*Argon2i) Name

func (h *Argon2i) Name() string

type Argon2id

type Argon2id struct {
	// contains filtered or unexported fields
}

func (Argon2id) Compare

func (h Argon2id) Compare(hash, pass []byte) (equal bool, err error)

func (*Argon2id) Configure

func (h *Argon2id) Configure(config interface{}) error

func (Argon2id) Hash

func (h Argon2id) Hash(pass []byte) (hash []byte, err error)

func (Argon2id) HashSalt

func (h Argon2id) HashSalt(pass, salt []byte) (hash []byte, err error)

func (*Argon2id) Name

func (h *Argon2id) Name() string

type Bcrypt

type Bcrypt struct {
	// contains filtered or unexported fields
}

func (Bcrypt) Compare

func (h Bcrypt) Compare(hash, pass []byte) (equal bool, err error)

func (*Bcrypt) Configure

func (h *Bcrypt) Configure(config interface{}) error

func (Bcrypt) Hash

func (h Bcrypt) Hash(pass []byte) (hash []byte, err error)

func (*Bcrypt) Name

func (h *Bcrypt) Name() string

type BcryptConfig

type BcryptConfig struct {
	Cost int
}

type SaltConfig

type SaltConfig struct {
	Length int
}

type Scrypt

type Scrypt struct {
	Config ScryptConfig
}

func (*Scrypt) Compare

func (h *Scrypt) Compare(hash, pass []byte) (equal bool, err error)

func (*Scrypt) Configure

func (h *Scrypt) Configure(config interface{}) error

func (Scrypt) Hash

func (h Scrypt) Hash(pass []byte) (hash []byte, err error)

func (Scrypt) HashSalt

func (h Scrypt) HashSalt(pass, salt []byte) (hash []byte, err error)

func (Scrypt) Name

func (h Scrypt) Name() string

type ScryptConfig

type ScryptConfig struct {
	N      int
	R      int
	P      int
	KeyLen int
	Salt   SaltConfig
}

Jump to

Keyboard shortcuts

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