mirror of
https://github.com/wneessen/apg-go.git
synced 2024-11-25 15:20:50 +01:00
#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:
parent
69bb1e4cb7
commit
1e1ae45e74
2 changed files with 50 additions and 14 deletions
|
@ -14,7 +14,15 @@ func main() {
|
|||
c := apg.NewConfig()
|
||||
|
||||
// Configure and parse the CLI flags
|
||||
// See usage() for flag details
|
||||
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.MaxLength, "x", c.MaxLength, "")
|
||||
flag.StringVar(&ms, "M", "", "")
|
||||
|
@ -22,11 +30,34 @@ func main() {
|
|||
flag.Usage = usage
|
||||
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 != "" {
|
||||
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} {
|
||||
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)
|
||||
|
@ -57,13 +88,15 @@ Options:
|
|||
-x LENGTH Maximum length of the password to be generated (Default: 20)
|
||||
-n NUMBER Amount of password to be generated (Default: 6)
|
||||
-E CHARS List of characters to be excluded in the generated password
|
||||
-M [LUNSHClunshc] New style password parameters (upper case: on, lower case: off)
|
||||
-L Use lower case characters in passwords (Default: on)
|
||||
-U Use upper case characters in passwords (Default: on)
|
||||
-N Use numeric characters in passwords (Default: on)
|
||||
-S Use special characters in passwords (Default: off)
|
||||
-M [LUNSHClunshc] New style password parameters
|
||||
- Note: new-style flags have higher priority than any of the old-style flags
|
||||
-L Toggle lower case characters in passwords (Default: on)
|
||||
-U Toggle upper case characters in passwords (Default: on)
|
||||
-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)
|
||||
-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)
|
||||
-p Check the HIBP database if the generated passwords was found in a leak before (Default: off)
|
||||
- Note: this feature requires internet connectivity
|
||||
|
@ -71,6 +104,5 @@ Options:
|
|||
-v Show version string
|
||||
|
||||
`
|
||||
|
||||
_, _ = os.Stderr.WriteString(ut)
|
||||
}
|
||||
|
|
|
@ -6,6 +6,9 @@ const (
|
|||
DefaultMinLength int64 = 12
|
||||
// DefaultMaxLength reflects the default maximum length of a generated password
|
||||
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 int64 = 6
|
||||
)
|
||||
|
@ -18,8 +21,8 @@ type Config struct {
|
|||
MaxLength int64
|
||||
// MinLength sets the minimum length for a generated password
|
||||
MinLength int64
|
||||
// Modes holds the different character modes for the Random algorithm
|
||||
Modes ModeMask
|
||||
// Mode holds the different character modes for the Random algorithm
|
||||
Mode ModeMask
|
||||
// NumberPass sets the number of passwords that are generated
|
||||
// and returned by the generator
|
||||
NumberPass int64
|
||||
|
@ -34,6 +37,7 @@ func NewConfig(o ...Option) *Config {
|
|||
c := &Config{
|
||||
MaxLength: DefaultMaxLength,
|
||||
MinLength: DefaultMinLength,
|
||||
Mode: DefaultMode,
|
||||
NumberPass: DefaultNumberPass,
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue