password

package module
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Sep 14, 2021 License: MIT Imports: 14 Imported by: 0

README

password

GoDoc Go.Dev reference codecov Action Status Go Report Card Licence Tag

Usage

Start using it

Download and install it:

    go get github.com/things-go/password

Import it in your code:

    import "github.com/things-go/password"
Example
package main

import (
	"log"

	"github.com/things-go/password"
)

func main() {
	// var pass password.Crypt = new(password.SCrypt)
	// var pass password.Crypt = new(password.Simple)
	var pass password.Crypt = new(password.BCrypt)
	origPasswd := "123456"

	hashPasswd, err := pass.GenerateFromPassword(origPasswd)
	if err != nil {
		log.Println(err)
		return
	}

	err = pass.CompareHashAndPassword(hashPasswd, origPasswd)
	if err != nil {
		log.Println(err)
		return
	}
	log.Println("hash passwd: ", hashPasswd)
	log.Println("success")

	// default use lower, upper and digit
	cm := password.NewComplexity(password.WithMeet())
	newPass := cm.Generate(6)
	if cm.IsComplexEnough(newPass) {
		log.Println("complex enough: ", newPass)
	}
	if !cm.IsComplexEnough("abc123") {
		log.Println("not complex enough")
	}
}

License

This project is under MIT License. See the LICENSE file for the full license text.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrCompareFailed = errors.New("crypt compare failed")

ErrCompareFailed compare failed

Functions

This section is empty.

Types

type BCrypt

type BCrypt struct{}

BCrypt password encryption

func (BCrypt) CompareHashAndPassword

func (sf BCrypt) CompareHashAndPassword(hashedPassword, password string) error

CompareHashAndPassword password hash verification

func (BCrypt) GenerateFromPassword

func (sf BCrypt) GenerateFromPassword(password string) (string, error)

GenerateFromPassword password hash encryption

type Complexity added in v0.0.3

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

Complexity setting

func NewComplexity added in v0.0.3

func NewComplexity(opts ...Option) *Complexity

NewComplexity new complexity with option default use lower, upper and digit to generate a random password, and not meets complexity.

func (*Complexity) Generate added in v0.0.3

func (sf *Complexity) Generate(n int) string

Generate a random password which is complex enough.

func (*Complexity) IsComplexEnough added in v0.0.3

func (sf *Complexity) IsComplexEnough(pwd string) bool

IsComplexEnough return True if password meets complexity settings

type Crypt

type Crypt interface {
	GenerateFromPassword(password string) (string, error)
	CompareHashAndPassword(hashedPassword, password string) error
}

Crypt crypt interface

type Option added in v0.0.3

type Option func(*complexityConfig)

Option for Complexity

func WithAll added in v0.0.3

func WithAll() Option

WithAll use lower upper digit spec and enable meet complexity

func WithDigit added in v0.0.3

func WithDigit() Option

WithDigit use digit

func WithLower added in v0.0.3

func WithLower() Option

WithLower use lower

func WithLowerUpper added in v0.0.3

func WithLowerUpper() Option

WithLowerUpper use lower upper

func WithLowerUpperDigit added in v0.0.3

func WithLowerUpperDigit() Option

WithLowerUpperDigit use lower upper digit

func WithMeet added in v0.0.3

func WithMeet() Option

WithMeet enable meet complexity

func WithSpec added in v0.0.3

func WithSpec() Option

WithSpec use spec

func WithUpper added in v0.0.3

func WithUpper() Option

WithUpper use upper

type SCrypt

type SCrypt struct{}

SCrypt password encryption

func (SCrypt) CompareHashAndPassword

func (SCrypt) CompareHashAndPassword(hashedPassword, password string) error

CompareHashAndPassword password hash verification

func (SCrypt) GenerateFromPassword

func (SCrypt) GenerateFromPassword(password string) (string, error)

GenerateFromPassword password hash encryption

type Simple

type Simple struct{}

Simple password encryption

func (Simple) CompareHashAndPassword

func (sf Simple) CompareHashAndPassword(hashedPassword, password string) error

CompareHashAndPassword Compare password hash verification

func (Simple) GenerateFromPassword

func (sf Simple) GenerateFromPassword(password string) (string, error)

GenerateFromPassword generate password hash encryption 加盐法

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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