#53 Refactor password generator to include additional modes

Updated the password generator to include default modes for characters along with options to enable complex password mode and toggle specific character types in passwords from the command line. This allows for greater customization and more user control in password generation, especially useful for applications with unique password requirements."
This commit is contained in:
Winni Neessen 2023-08-05 14:47:30 +02:00
parent 69bb1e4cb7
commit 1e1ae45e74
Signed by: wneessen
GPG key ID: 5F3AF39B820C119D
2 changed files with 50 additions and 14 deletions

View file

@ -14,7 +14,15 @@ func main() {
c := apg.NewConfig() c := apg.NewConfig()
// Configure and parse the CLI flags // Configure and parse the CLI flags
// See usage() for flag details
var ms string var ms string
var co, hr, lc, nu, sp, uc bool
flag.BoolVar(&lc, "L", false, "")
flag.BoolVar(&uc, "U", false, "")
flag.BoolVar(&nu, "N", false, "")
flag.BoolVar(&sp, "S", false, "")
flag.BoolVar(&co, "C", false, "")
flag.BoolVar(&hr, "H", false, "")
flag.Int64Var(&c.MinLength, "m", c.MinLength, "") flag.Int64Var(&c.MinLength, "m", c.MinLength, "")
flag.Int64Var(&c.MaxLength, "x", c.MaxLength, "") flag.Int64Var(&c.MaxLength, "x", c.MaxLength, "")
flag.StringVar(&ms, "M", "", "") flag.StringVar(&ms, "M", "", "")
@ -22,11 +30,34 @@ func main() {
flag.Usage = usage flag.Usage = usage
flag.Parse() flag.Parse()
// Old style character modes
if hr {
c.Mode = apg.MaskToggleMode(c.Mode, apg.ModeHumanReadable)
}
if lc {
c.Mode = apg.MaskToggleMode(c.Mode, apg.ModeLowerCase)
}
if uc {
c.Mode = apg.MaskToggleMode(c.Mode, apg.ModeUpperCase)
}
if nu {
c.Mode = apg.MaskToggleMode(c.Mode, apg.ModeNumber)
}
if sp {
c.Mode = apg.MaskToggleMode(c.Mode, apg.ModeSpecial)
}
if co {
c.Mode = apg.MaskSetMode(c.Mode, apg.ModeLowerCase|apg.ModeNumber|
apg.ModeSpecial|apg.ModeUpperCase)
c.Mode = apg.MaskClearMode(c.Mode, apg.ModeHumanReadable)
}
// New style character modes (has higher priority than the old style modes)
if ms != "" { if ms != "" {
c.Modes = apg.ModesFromFlags(ms) c.Mode = apg.ModesFromFlags(ms)
} }
for _, m := range []apg.Mode{apg.ModeHumanReadable, apg.ModeLowerCase, apg.ModeNumber, apg.ModeSpecial, apg.ModeUpperCase} { for _, m := range []apg.Mode{apg.ModeHumanReadable, apg.ModeLowerCase, apg.ModeNumber, apg.ModeSpecial, apg.ModeUpperCase} {
fmt.Printf("%s: %t\n", m, apg.MaskHasMode(c.Modes, m)) fmt.Printf("%s: %t\n", m, apg.MaskHasMode(c.Mode, m))
} }
/* /*
g := apg.New(c) g := apg.New(c)
@ -51,26 +82,27 @@ apg [-a <algo>] [-m <length>] [-x <length>] [-L] [-U] [-N] [-S] [-H] [-C]
Options: Options:
-a ALGORITH Choose the password generation algorithm (Default: 1) -a ALGORITH Choose the password generation algorithm (Default: 1)
- 0: pronounceable password generation (koremutake syllables) - 0: pronounceable password generation (koremutake syllables)
- 1: random password generation according to password modes/flags - 1: random password generation according to password modes/flags
-m LENGTH Minimum length of the password to be generated (Default: 12) -m LENGTH Minimum length of the password to be generated (Default: 12)
-x LENGTH Maximum length of the password to be generated (Default: 20) -x LENGTH Maximum length of the password to be generated (Default: 20)
-n NUMBER Amount of password to be generated (Default: 6) -n NUMBER Amount of password to be generated (Default: 6)
-E CHARS List of characters to be excluded in the generated password -E CHARS List of characters to be excluded in the generated password
-M [LUNSHClunshc] New style password parameters (upper case: on, lower case: off) -M [LUNSHClunshc] New style password parameters
-L Use lower case characters in passwords (Default: on) - Note: new-style flags have higher priority than any of the old-style flags
-U Use upper case characters in passwords (Default: on) -L Toggle lower case characters in passwords (Default: on)
-N Use numeric characters in passwords (Default: on) -U Toggle upper case characters in passwords (Default: on)
-S Use special characters in passwords (Default: off) -N Toggle numeric characters in passwords (Default: on)
-S Toggle special characters in passwords (Default: off)
-H Avoid ambiguous characters in passwords (i. e.: 1, l, I, O, 0) (Default: off) -H Avoid ambiguous characters in passwords (i. e.: 1, l, I, O, 0) (Default: off)
-C Enable complex password mode (implies -L -U -N -S and disables -H) (Default: off) -C Enable complex password mode (implies -L -U -N -S and disables -H)
- Note: this flag has higher priority than the other old-style flags
-l Spell generated passwords in phonetic alphabet (Default: off) -l Spell generated passwords in phonetic alphabet (Default: off)
-p Check the HIBP database if the generated passwords was found in a leak before (Default: off) -p Check the HIBP database if the generated passwords was found in a leak before (Default: off)
- Note: this feature requires internet connectivity - Note: this feature requires internet connectivity
-h Show this help text -h Show this help text
-v Show version string -v Show version string
` `
_, _ = os.Stderr.WriteString(ut) _, _ = os.Stderr.WriteString(ut)
} }

View file

@ -6,6 +6,9 @@ const (
DefaultMinLength int64 = 12 DefaultMinLength int64 = 12
// DefaultMaxLength reflects the default maximum length of a generated password // DefaultMaxLength reflects the default maximum length of a generated password
DefaultMaxLength int64 = 20 DefaultMaxLength int64 = 20
// DefaultMode sets the default character set mode bitmask to a combination of
// lower- and upper-case characters as well as numbers
DefaultMode ModeMask = ModeLowerCase | ModeNumber | ModeUpperCase
// DefaultNumberPass reflects the default amount of passwords returned by the generator // DefaultNumberPass reflects the default amount of passwords returned by the generator
DefaultNumberPass int64 = 6 DefaultNumberPass int64 = 6
) )
@ -18,8 +21,8 @@ type Config struct {
MaxLength int64 MaxLength int64
// MinLength sets the minimum length for a generated password // MinLength sets the minimum length for a generated password
MinLength int64 MinLength int64
// Modes holds the different character modes for the Random algorithm // Mode holds the different character modes for the Random algorithm
Modes ModeMask Mode ModeMask
// NumberPass sets the number of passwords that are generated // NumberPass sets the number of passwords that are generated
// and returned by the generator // and returned by the generator
NumberPass int64 NumberPass int64
@ -34,6 +37,7 @@ func NewConfig(o ...Option) *Config {
c := &Config{ c := &Config{
MaxLength: DefaultMaxLength, MaxLength: DefaultMaxLength,
MinLength: DefaultMinLength, MinLength: DefaultMinLength,
Mode: DefaultMode,
NumberPass: DefaultNumberPass, NumberPass: DefaultNumberPass,
} }