mirror of
https://github.com/wneessen/apg-go.git
synced 2024-11-22 05:40:51 +01:00
Refactor variable names and improve code readability in apg.go
Updated several variable names such as 'c' to 'config' and 'al' to 'algorithm' in order to improve code readability and understanding. An additional 'SpellPassword' option was added to provide users the ability to hear their passwords spelled out, enhancing functionality. This improves readability and user experience.
This commit is contained in:
parent
0ad5f4a74d
commit
4219a27007
1 changed files with 63 additions and 54 deletions
117
cmd/apg/apg.go
117
cmd/apg/apg.go
|
@ -18,102 +18,111 @@ const MinimumAmountTooHigh = "WARNING: You have selected a minimum amount of cha
|
||||||
"the job. Please consider lowering the value.\n\n"
|
"the job. Please consider lowering the value.\n\n"
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
c := apg.NewConfig()
|
config := apg.NewConfig()
|
||||||
|
|
||||||
// Configure and parse the CLI flags
|
// Configure and parse the CLI flags
|
||||||
// See usage() for flag details
|
// See usage() for flag details
|
||||||
var al int
|
var algorithm int
|
||||||
var ms string
|
var modeString string
|
||||||
var co, hr, lc, nu, sp, uc bool
|
var complexPass, humanReadable, lowerCase, numeric, special, upperCase bool
|
||||||
flag.IntVar(&al, "a", 1, "")
|
flag.IntVar(&algorithm, "a", 1, "")
|
||||||
flag.BoolVar(&lc, "L", false, "")
|
flag.BoolVar(&lowerCase, "L", false, "")
|
||||||
flag.Int64Var(&c.MinLowerCase, "mL", c.MinLowerCase, "")
|
flag.Int64Var(&config.MinLowerCase, "mL", config.MinLowerCase, "")
|
||||||
flag.BoolVar(&uc, "U", false, "")
|
flag.BoolVar(&upperCase, "U", false, "")
|
||||||
flag.Int64Var(&c.MinUpperCase, "mU", c.MinUpperCase, "")
|
flag.Int64Var(&config.MinUpperCase, "mU", config.MinUpperCase, "")
|
||||||
flag.BoolVar(&nu, "N", false, "")
|
flag.BoolVar(&numeric, "N", false, "")
|
||||||
flag.Int64Var(&c.MinNumeric, "mN", c.MinNumeric, "")
|
flag.Int64Var(&config.MinNumeric, "mN", config.MinNumeric, "")
|
||||||
flag.BoolVar(&sp, "S", false, "")
|
flag.BoolVar(&special, "S", false, "")
|
||||||
flag.Int64Var(&c.MinSpecial, "mS", c.MinSpecial, "")
|
flag.Int64Var(&config.MinSpecial, "mS", config.MinSpecial, "")
|
||||||
flag.BoolVar(&co, "C", false, "")
|
flag.BoolVar(&complexPass, "C", false, "")
|
||||||
flag.BoolVar(&hr, "H", false, "")
|
flag.BoolVar(&humanReadable, "H", false, "")
|
||||||
flag.Int64Var(&c.FixedLength, "f", 0, "")
|
flag.Int64Var(&config.FixedLength, "f", 0, "")
|
||||||
flag.Int64Var(&c.MinLength, "m", c.MinLength, "")
|
flag.Int64Var(&config.MinLength, "m", config.MinLength, "")
|
||||||
flag.Int64Var(&c.MaxLength, "x", c.MaxLength, "")
|
flag.Int64Var(&config.MaxLength, "x", config.MaxLength, "")
|
||||||
flag.StringVar(&ms, "M", "", "")
|
flag.StringVar(&modeString, "M", "", "")
|
||||||
flag.Int64Var(&c.NumberPass, "n", c.NumberPass, "")
|
flag.Int64Var(&config.NumberPass, "n", config.NumberPass, "")
|
||||||
|
flag.BoolVar(&config.SpellPassword, "l", false, "")
|
||||||
flag.Usage = usage
|
flag.Usage = usage
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
// Old style character modes
|
// Old style character modes
|
||||||
if hr {
|
if humanReadable {
|
||||||
c.Mode = apg.MaskToggleMode(c.Mode, apg.ModeHumanReadable)
|
config.Mode = apg.MaskToggleMode(config.Mode, apg.ModeHumanReadable)
|
||||||
}
|
}
|
||||||
if lc {
|
if lowerCase {
|
||||||
c.Mode = apg.MaskToggleMode(c.Mode, apg.ModeLowerCase)
|
config.Mode = apg.MaskToggleMode(config.Mode, apg.ModeLowerCase)
|
||||||
}
|
}
|
||||||
if uc {
|
if upperCase {
|
||||||
c.Mode = apg.MaskToggleMode(c.Mode, apg.ModeUpperCase)
|
config.Mode = apg.MaskToggleMode(config.Mode, apg.ModeUpperCase)
|
||||||
}
|
}
|
||||||
if nu {
|
if numeric {
|
||||||
c.Mode = apg.MaskToggleMode(c.Mode, apg.ModeNumeric)
|
config.Mode = apg.MaskToggleMode(config.Mode, apg.ModeNumeric)
|
||||||
}
|
}
|
||||||
if sp {
|
if special {
|
||||||
c.Mode = apg.MaskToggleMode(c.Mode, apg.ModeSpecial)
|
config.Mode = apg.MaskToggleMode(config.Mode, apg.ModeSpecial)
|
||||||
}
|
}
|
||||||
if co {
|
if complexPass {
|
||||||
c.Mode = apg.MaskSetMode(c.Mode, apg.ModeLowerCase|apg.ModeNumeric|
|
config.Mode = apg.MaskSetMode(config.Mode, apg.ModeLowerCase|apg.ModeNumeric|
|
||||||
apg.ModeSpecial|apg.ModeUpperCase)
|
apg.ModeSpecial|apg.ModeUpperCase)
|
||||||
c.Mode = apg.MaskClearMode(c.Mode, apg.ModeHumanReadable)
|
config.Mode = apg.MaskClearMode(config.Mode, apg.ModeHumanReadable)
|
||||||
}
|
}
|
||||||
|
|
||||||
// New style character modes (has higher priority than the old style modes)
|
// New style character modes (has higher priority than the old style modes)
|
||||||
if ms != "" {
|
if modeString != "" {
|
||||||
c.Mode = apg.ModesFromFlags(ms)
|
config.Mode = apg.ModesFromFlags(modeString)
|
||||||
}
|
}
|
||||||
|
|
||||||
// For the "minimum amount of" modes we need to imply at the type
|
// For the "minimum amount of" modes we need to imply at the type
|
||||||
// of character mode is set
|
// of character mode is set
|
||||||
if c.MinLowerCase > 0 {
|
if config.MinLowerCase > 0 {
|
||||||
if float64(c.MinLength)/2 < float64(c.MinNumeric) {
|
if float64(config.MinLength)/2 < float64(config.MinNumeric) {
|
||||||
_, _ = os.Stderr.WriteString(MinimumAmountTooHigh)
|
_, _ = os.Stderr.WriteString(MinimumAmountTooHigh)
|
||||||
}
|
}
|
||||||
c.Mode = apg.MaskSetMode(c.Mode, apg.ModeLowerCase)
|
config.Mode = apg.MaskSetMode(config.Mode, apg.ModeLowerCase)
|
||||||
}
|
}
|
||||||
if c.MinNumeric > 0 {
|
if config.MinNumeric > 0 {
|
||||||
if float64(c.MinLength)/2 < float64(c.MinLowerCase) {
|
if float64(config.MinLength)/2 < float64(config.MinLowerCase) {
|
||||||
_, _ = os.Stderr.WriteString(MinimumAmountTooHigh)
|
_, _ = os.Stderr.WriteString(MinimumAmountTooHigh)
|
||||||
}
|
}
|
||||||
c.Mode = apg.MaskSetMode(c.Mode, apg.ModeNumeric)
|
config.Mode = apg.MaskSetMode(config.Mode, apg.ModeNumeric)
|
||||||
}
|
}
|
||||||
if c.MinSpecial > 0 {
|
if config.MinSpecial > 0 {
|
||||||
if float64(c.MinLength)/2 < float64(c.MinSpecial) {
|
if float64(config.MinLength)/2 < float64(config.MinSpecial) {
|
||||||
_, _ = os.Stderr.WriteString(MinimumAmountTooHigh)
|
_, _ = os.Stderr.WriteString(MinimumAmountTooHigh)
|
||||||
}
|
}
|
||||||
c.Mode = apg.MaskSetMode(c.Mode, apg.ModeSpecial)
|
config.Mode = apg.MaskSetMode(config.Mode, apg.ModeSpecial)
|
||||||
}
|
}
|
||||||
if c.MinUpperCase > 0 {
|
if config.MinUpperCase > 0 {
|
||||||
if float64(c.MinLength)/2 < float64(c.MinUpperCase) {
|
if float64(config.MinLength)/2 < float64(config.MinUpperCase) {
|
||||||
_, _ = os.Stderr.WriteString(MinimumAmountTooHigh)
|
_, _ = os.Stderr.WriteString(MinimumAmountTooHigh)
|
||||||
}
|
}
|
||||||
c.Mode = apg.MaskSetMode(c.Mode, apg.ModeUpperCase)
|
config.Mode = apg.MaskSetMode(config.Mode, apg.ModeUpperCase)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if algorithm is supported
|
// Check if algorithm is supported
|
||||||
c.Algorithm = apg.IntToAlgo(al)
|
config.Algorithm = apg.IntToAlgo(algorithm)
|
||||||
if c.Algorithm == apg.AlgoUnsupported {
|
if config.Algorithm == apg.AlgoUnsupported {
|
||||||
_, _ = fmt.Fprintf(os.Stderr, "unsupported algorithm value: %d\n", al)
|
_, _ = fmt.Fprintf(os.Stderr, "unsupported algorithm value: %d\n", algorithm)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generate the password based on the given flags
|
// Generate the password based on the given flags
|
||||||
g := apg.New(c)
|
generator := apg.New(config)
|
||||||
for i := int64(0); i < c.NumberPass; i++ {
|
for i := int64(0); i < config.NumberPass; i++ {
|
||||||
p, err := g.Generate()
|
password, err := generator.Generate()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
_, _ = fmt.Fprintf(os.Stderr, "failed to generate password: %s\n", err)
|
_, _ = fmt.Fprintf(os.Stderr, "failed to generate password: %s\n", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
fmt.Println(p)
|
if config.SpellPassword {
|
||||||
|
spellPass, err := apg.Spell(password)
|
||||||
|
if err != nil {
|
||||||
|
_, _ = fmt.Fprintf(os.Stderr, "failed to spell password: %s\n", err)
|
||||||
|
}
|
||||||
|
fmt.Printf("%s (%s)\n", password, spellPass)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
fmt.Println(password)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue