zdpgo_password_generate

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Aug 30, 2022 License: MIT Imports: 16 Imported by: 0

README

zdpgo_password_generate

专用于生成密码的组件

版本历史

  • v0.1.0 2022/07/08 新增:密码生成,密码校验
  • v0.1.1 2022/08/30 新增:V2级别的密码生成和校验

使用示例

请查看 examples 目录

Documentation

Index

Constants

View Source
const (
	LengthWeak                uint = 6                                 // 弱密码长度
	LengthOK                  uint = 12                                // 一般密码长度
	LengthStrong              uint = 24                                // 强密码长度
	LengthVeryStrong          uint = 36                                // 高强密码长度
	DefaultLetterSet               = "abcdefghijklmnopqrstuvwxyz"      // 默认小写字母集合
	DefaultLetterAmbiguousSet      = "ijlo"                            // 默认相似字母集合
	DefaultNumberSet               = "0123456789"                      // 默认数字集合
	DefaultNumberAmbiguousSet      = "01"                              // 默认相似数字集合
	DefaultSymbolSet               = "!$%^&*()_+{}:@[];'#<>?,./|\\-=?" // 默认特殊符号集合
	DefaultSymbolAmbiguousSet      = "<>[](){}:;'/|\\,"                //默认转义符号集合
)

Variables

View Source
var (
	// ErrCiphertextVer indicates version sub-string mismatch normally; ex. "secBoxv1"
	ErrCiphertextVer = errors.New("Nonmatched ciphertext version")
	// ErrCiphertextFormat indicates input is not in expected format
	ErrCiphertextFormat = errors.New("Ciphertext input format not as expected")
	// ErrInvalidVersionUpdate indicates new version given not oldVersion + 1 or greater
	ErrInvalidVersionUpdate = errors.New("Invalid new version int, new master passphrase version must be greater than previous")
	// ErrPassphraseHashMismatch indicates invalid passphrase for supplied ciphertext
	ErrPassphraseHashMismatch = errors.New("Passphrase hash does not match supplied ciphertext")
	// ErrPassphraseLength indicates supplied passphrase is not at least MinLength
	ErrPassphraseLength = errors.New("Passphrase must be at least MinLength")
	// ErrSecretBoxDecryptFail indicates SecretBox decryption could not be completed
	ErrSecretBoxDecryptFail = errors.New("SecretBox decryption failed")
	// ErrScryptParamN indicates ScryptParams:N out of acceptable range
	ErrScryptParamN = errors.New("Given Scrypt (N) cost factor out of acceptable range")
	// ErrScryptParamR indicates ScryptParams:r out of acceptable range
	ErrScryptParamR = errors.New("Given Scrypt (r) cost factor out of acceptable range")
	// ErrScryptParamP indicates ScryptParams:p out of acceptable range
	ErrScryptParamP = errors.New("Given Scrypt (p) cost factor out of acceptable range")
)
View Source
var (
	// DefaultConfig 默认密码配置
	DefaultConfig = Config{
		Length:                     LengthStrong,
		IncludeSymbols:             true,
		IncludeNumbers:             true,
		IncludeLowercaseLetters:    true,
		IncludeUppercaseLetters:    true,
		ExcludeSimilarCharacters:   true,
		ExcludeAmbiguousCharacters: true,
	}

	// ErrConfigIsEmpty 密码配置为空错误
	ErrConfigIsEmpty = errors.New("config is empty")

	// 默认密码生成器
	DefaultGenerator = New()
)
View Source
var (
	// MinLength changes the minimum passphrase and master passphrase length accepted
	MinLength = 8
	// DefaultParams defines Scrypt Parameters
	DefaultParams = ScryptParams{N: 16384, R: 8, P: 1}
)

Functions

func Benchmark

func Benchmark(params ScryptParams) (seconds float64, err error)

Benchmark takes ScryptParams and returns the number of seconds elapsed as a float64 and error

func CheckV2

func CheckV2(origin, passwordStr string) bool

CheckV2 校验V2级别的密码 @param origin 原始密码 @param passwordStr 加密后的密码 @return 密码是否正确

func GenerateV2

func GenerateV2(origin string) string

GenerateV2 生成V2级别的密码 @param origin 要加密的字符串 @return 加密后的密码字符串

func GetHashVersion

func GetHashVersion(ciphertext string) (version int, err error)

GetHashVersion takes ciphertext string and returns goSecretBoxPassword version as int and error.

func GetMasterVersion

func GetMasterVersion(ciphertext string) (version int, err error)

GetMasterVersion takes ciphertext string and returns master passphrase version as int and error.

func Hash

func Hash(userpass, masterpass string, version int, userparams, masterparams ScryptParams) (pwHashOut string, err error)

Hash takes passphrase ,masterpassphrase as strings, version indicator as int, and userparams and masterparams as ScryptParams and returns up to 225 char ciphertext string and error - ex. password.Hash("password1234", "masterpassphrase", 0, ScryptParams{N: 32768, R: 16, P: 1}, DefaultParams)

func UpdateMaster

func UpdateMaster(newMaster, oldMaster string, newVersion int, ciphertext string, masterparams ScryptParams) (pwHashOut string, err error)

UpdateMaster takes new master passphrase, old master passphrase as string, new version as int, cipertext as string, and new ScryptParams. It returns and updated hash output string and error.

func Verify

func Verify(userpass, masterpass, ciphertext string) error

Verify takes passphrase, masterpassphrase and ciphertext as strings and returns error if verification fails, else returns nil upon success

Types

type Config

type Config struct {
	Length                     uint   `yaml:"length" json:"length"`                                             // 密码长度
	CharacterSet               string `yaml:"character_set" json:"character_set"`                               // 字符池子,从池子里面取密码字符
	IncludeSymbols             bool   `yaml:"include_symbols" json:"include_symbols"`                           // 是否包含标志,比如!"£*
	IncludeNumbers             bool   `yaml:"include_numbers" json:"include_numbers"`                           // 是否包含数字
	IncludeLowercaseLetters    bool   `yaml:"include_lowercase_letters" json:"include_lowercase_letters"`       // 是否包含小写字母
	IncludeUppercaseLetters    bool   `yaml:"include_uppercase_letters" json:"include_uppercase_letters"`       // 是否包含大写字母
	ExcludeSimilarCharacters   bool   `yaml:"exclude_similar_characters" json:"exclude_similar_characters"`     // 是否包含相似的字符,比如i1jIo0
	ExcludeAmbiguousCharacters bool   `yaml:"exclude_ambiguous_characters" json:"exclude_ambiguous_characters"` // 是否包含特殊转义符号<>{}[]()/|\
}

Config 生成密码的配置

type Generator

type Generator struct {
	Config *Config
}

Generator 密码生成器

func New

func New() *Generator

func NewWithConfig

func NewWithConfig(config *Config) *Generator

NewWithConfig 返回密码生成器

func (Generator) Generate

func (g Generator) Generate() (*string, error)

Generate generates one password with length set in the config

func (Generator) GenerateByLength

func (g Generator) GenerateByLength(length uint) string

GenerateByLength 生成指定长度的命名

func (Generator) GenerateByOK

func (g Generator) GenerateByOK() string

GenerateByOK 生成普通密码

func (Generator) GenerateByStrong

func (g Generator) GenerateByStrong() string

GenerateByStrong 生成强密码

func (Generator) GenerateByVeryStrong

func (g Generator) GenerateByVeryStrong() string

GenerateByVeryStrong 生成超强密码

func (Generator) GenerateByWeak

func (g Generator) GenerateByWeak() string

GenerateByWeak 生成弱密码

func (Generator) GenerateMany

func (g Generator) GenerateMany(amount uint) ([]string, error)

GenerateMany 批量生产密码

func (Generator) GenerateManyWithLength

func (g Generator) GenerateManyWithLength(amount, length uint) ([]string, error)

GenerateManyWithLength generates multiple passwords with set length

func (Generator) GenerateWithLength

func (g Generator) GenerateWithLength(length uint) (*string, error)

GenerateWithLength generate one password with set length

type ScryptParams

type ScryptParams struct {
	N int
	R int
	P int
}

ScryptParams sets the Scrypt devivation parameters used for hashing

func GetParams

func GetParams(ciphertext string) (userParams, masterParams ScryptParams, err error)

GetParams takes ciphertext string, returns user and master parameters and error. This may be useful for upgrading.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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