Added option to exclude certain characters in password generation

The code now supports the configuration parameter 'ExcludeChars', which gives users the ability to specify particular characters that should not be included in the generated passwords. This addition was fully implemented both on config.go and random.go, while the relevant option flag was also added to apg.go for user interaction.
This commit is contained in:
Winni Neessen 2024-03-12 20:31:27 +01:00
parent 2022e0953d
commit 2143ca99f5
Signed by: wneessen
GPG key ID: 5F3AF39B820C119D
3 changed files with 16 additions and 0 deletions

View file

@ -46,6 +46,7 @@ func main() {
flag.BoolVar(&config.SpellPronounceable, "t", false, "")
flag.BoolVar(&config.CheckHIBP, "p", false, "")
flag.BoolVar(&showVer, "v", false, "")
flag.StringVar(&config.ExcludeChars, "E", "", "")
flag.Usage = usage
flag.Parse()

View file

@ -20,6 +20,9 @@ type Config struct {
// CheckHIBP sets a flag if the generated password has to be checked
// against the HIBP pwned password database
CheckHIBP bool
// ExcludeChars is a list of characters that should be excluded from
// generated passwords
ExcludeChars string
// FixedLength sets a fixed length for generated passwords and ignores
// the MinLength and MaxLength values
FixedLength int64

View file

@ -6,6 +6,8 @@ import (
"errors"
"fmt"
"math/big"
"os"
"regexp"
"strings"
)
@ -93,6 +95,16 @@ func (g *Generator) GetCharRangeFromConfig() string {
charRange.WriteString(CharRangeAlphaUpper)
}
}
if g.config.ExcludeChars != "" {
rex, err := regexp.Compile("[" + regexp.QuoteMeta(g.config.ExcludeChars) + "]")
if err == nil {
newRange := rex.ReplaceAllLiteralString(charRange.String(), "")
charRange.Reset()
charRange.WriteString(newRange)
} else {
_, _ = fmt.Fprintf(os.Stderr, "failed to exclude characters: %s\n", err)
}
}
return charRange.String()
}