args... still needs charset minimums (how?)
This commit is contained in:
@@ -2,12 +2,86 @@ package pwgenerator
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"r00t2.io/goutils/multierr"
|
||||
)
|
||||
|
||||
// Generate generates a list of passwords for a GenOpts.
|
||||
func (o *GenOpts) Generate() (passwords []string, err error) {
|
||||
|
||||
// TODO
|
||||
var passwds []string = make([]string, o.Count)
|
||||
var charset CharSet = o.Chars()
|
||||
var errs *multierr.MultiError = multierr.NewMultiError(nil)
|
||||
|
||||
if o.Count == 0 {
|
||||
o.Count = DefCount
|
||||
}
|
||||
if o.LengthMax == 0 {
|
||||
o.LengthMax = DefMaxLen
|
||||
}
|
||||
|
||||
for idx, _ := range passwds {
|
||||
if passwds[idx], err = o.generatePassword(charset); err != nil {
|
||||
errs.AddError(err)
|
||||
err = nil
|
||||
}
|
||||
}
|
||||
|
||||
passwords = passwds
|
||||
|
||||
if !errs.IsEmpty() {
|
||||
err = errs
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// generatePassword generates a single password from CharSet c.
|
||||
func (o *GenOpts) generatePassword(c CharSet) (password string, err error) {
|
||||
|
||||
var maxMin uint
|
||||
var trueMinLen uint
|
||||
var passLenGap uint
|
||||
var passLen int
|
||||
var passAlloc []rune
|
||||
|
||||
// Sanity checks/error conditions.
|
||||
maxMin = o.CountUpper + o.CountLower + o.CountSymbols + o.CountExtended
|
||||
if maxMin > o.LengthMax {
|
||||
err = ErrTooSmall
|
||||
return
|
||||
}
|
||||
if o.LengthMin > o.LengthMax {
|
||||
err = ErrSwitchedLenLimits
|
||||
return
|
||||
}
|
||||
|
||||
// Defaults.
|
||||
trueMinLen = o.LengthMin
|
||||
if trueMinLen == 0 {
|
||||
trueMinLen = DefMinLin
|
||||
}
|
||||
|
||||
// Get a fixed password length...
|
||||
passLenGap = o.LengthMax - trueMinLen
|
||||
if passLen, err = saferRandInt(int(passLenGap)); err != nil {
|
||||
return
|
||||
}
|
||||
passLen = passLen + int(trueMinLen) // (We need to re-add; it was subtracted above to get a zero-shifted number for start in saferRandInt.)
|
||||
// And make the rune slice of that length.
|
||||
passAlloc = make([]rune, passLen)
|
||||
|
||||
for _, idx := range passAlloc {
|
||||
var char Char
|
||||
|
||||
if char, err = c.RandChar(); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
passAlloc[idx] = rune(char)
|
||||
}
|
||||
|
||||
password = string(passAlloc)
|
||||
|
||||
return
|
||||
}
|
||||
@@ -35,6 +109,8 @@ func (o *GenOpts) Chars() (chars CharSet) {
|
||||
chars = append(chars, extendedSymbols...)
|
||||
}
|
||||
|
||||
// TODO: Count* fields
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user