done, should be good to go

This commit is contained in:
brent s. 2022-03-04 01:17:10 -05:00
parent 1c0481824e
commit 8562241d52
Signed by: bts
GPG Key ID: 8C004C2F93481F6B
3 changed files with 64 additions and 34 deletions

4
TODO
View File

@ -1,5 +1,9 @@
- whitespace (specifically, ' ') - whitespace (specifically, ' ')


- "quick-format" options - json-safe/escaped, mysql-safe/escaped, etc.

- pretter printing for errors in main?

- "Human Readability"? - "Human Readability"?


- Hash/Salted hash generator - Hash/Salted hash generator

View File

@ -6,6 +6,7 @@ import (


var ( var (
ErrBadType error = errors.New("cannot typeswitch; unsupported type") ErrBadType error = errors.New("cannot typeswitch; unsupported type")
ErrEmptyCharsets error = errors.New("empty character set/no character sets selected")
ErrIncompatCharsetFilter error = errors.New("the selected minimum requirements are not possible with selected/enabled charsets") ErrIncompatCharsetFilter error = errors.New("the selected minimum requirements are not possible with selected/enabled charsets")
ErrSwitchedLenLimits error = errors.New("the max password length is shorter than the minimum password length") ErrSwitchedLenLimits error = errors.New("the max password length is shorter than the minimum password length")
ErrTooSmall error = errors.New("password max length too short for specified required chars") ErrTooSmall error = errors.New("password max length too short for specified required chars")

View File

@ -10,7 +10,7 @@ import (
func (o *GenOpts) Generate() (passwords []string, err error) { func (o *GenOpts) Generate() (passwords []string, err error) {


var passwds []string = make([]string, o.Count) var passwds []string = make([]string, o.Count)
var charset CharSet = o.Chars() var charset CharSet
var errs *multierr.MultiError = multierr.NewMultiError(nil) var errs *multierr.MultiError = multierr.NewMultiError(nil)


if o.Count == 0 { if o.Count == 0 {
@ -20,6 +20,14 @@ func (o *GenOpts) Generate() (passwords []string, err error) {
o.LengthMax = DefMaxLen o.LengthMax = DefMaxLen
} }


if err = o.sanChk(); err != nil {
return
}

if charset, err = o.Chars(); err != nil {
return
}

for idx, _ := range passwds { for idx, _ := range passwds {
if passwds[idx], err = o.generatePassword(charset); err != nil { if passwds[idx], err = o.generatePassword(charset); err != nil {
errs.AddError(err) errs.AddError(err)
@ -39,7 +47,6 @@ func (o *GenOpts) Generate() (passwords []string, err error) {
// generatePassword generates a single password from CharSet c (plus any minimum requirements). // generatePassword generates a single password from CharSet c (plus any minimum requirements).
func (o *GenOpts) generatePassword(c CharSet) (password string, err error) { func (o *GenOpts) generatePassword(c CharSet) (password string, err error) {


var maxMin uint
var trueMinLen uint var trueMinLen uint
var passLenGap uint var passLenGap uint
var passLen int var passLen int
@ -47,36 +54,7 @@ func (o *GenOpts) generatePassword(c CharSet) (password string, err error) {
var filter *selectFilter = o.getFilter() var filter *selectFilter = o.getFilter()
var isDisabled bool var isDisabled bool


// Sanity checks/error conditions. if err = o.sanChk(); err != nil {
if o.explicitCharset != nil && len(o.explicitCharset) != 0 {
if o.CountUpper > 0 && !o.Alpha {
err = ErrIncompatCharsetFilter
return
}
if o.CountLower > 0 && !o.Alpha {
err = ErrIncompatCharsetFilter
return
}
if o.CountNumbers > 0 && !o.Numeric {
err = ErrIncompatCharsetFilter
return
}
if o.CountSymbols > 0 && !o.Symbols {
err = ErrIncompatCharsetFilter
return
}
if o.CountExtended > 0 && !o.ExtendedSymbols {
err = ErrIncompatCharsetFilter
return
}
}
maxMin = o.CountUpper + o.CountLower + o.CountNumbers + o.CountSymbols + o.CountExtended
if maxMin > o.LengthMax {
err = ErrTooSmall
return
}
if o.LengthMin > o.LengthMax {
err = ErrSwitchedLenLimits
return return
} }


@ -204,10 +182,14 @@ func (o *GenOpts) generatePassword(c CharSet) (password string, err error) {
} }


// Chars returns the list of evaluated characters that would be used in a GenOpts. // Chars returns the list of evaluated characters that would be used in a GenOpts.
func (o *GenOpts) Chars() (chars CharSet) { func (o *GenOpts) Chars() (chars CharSet, err error) {


chars = make(CharSet, 0) chars = make(CharSet, 0)


if err = o.sanChk(); err != nil {
return
}

if o.explicitCharset != nil && len(o.explicitCharset) != 0 { if o.explicitCharset != nil && len(o.explicitCharset) != 0 {
chars = o.explicitCharset chars = o.explicitCharset
return return
@ -226,7 +208,10 @@ func (o *GenOpts) Chars() (chars CharSet) {
chars = append(chars, extendedSymbols...) chars = append(chars, extendedSymbols...)
} }


// TODO: Count* fields if chars == nil || len(chars) == 0 {
err = ErrEmptyCharsets
return
}


return return
} }
@ -304,3 +289,43 @@ func (o *GenOpts) getFilter() (f *selectFilter) {


return return
} }

func (o *GenOpts) sanChk() (err error) {

var maxMin uint

// Sanity checks/error conditions.
if o.explicitCharset != nil && len(o.explicitCharset) != 0 {
if o.CountUpper > 0 && !o.Alpha {
err = ErrIncompatCharsetFilter
return
}
if o.CountLower > 0 && !o.Alpha {
err = ErrIncompatCharsetFilter
return
}
if o.CountNumbers > 0 && !o.Numeric {
err = ErrIncompatCharsetFilter
return
}
if o.CountSymbols > 0 && !o.Symbols {
err = ErrIncompatCharsetFilter
return
}
if o.CountExtended > 0 && !o.ExtendedSymbols {
err = ErrIncompatCharsetFilter
return
}
}
maxMin = o.CountUpper + o.CountLower + o.CountNumbers + o.CountSymbols + o.CountExtended
if maxMin > o.LengthMax {
err = ErrTooSmall
return
}
if o.LengthMin > o.LengthMax {
err = ErrSwitchedLenLimits
return
}

return
}