From 2143ca99f58dbcf7cb40f748211ddf9f0c1b5d44 Mon Sep 17 00:00:00 2001 From: Winni Neessen Date: Tue, 12 Mar 2024 20:31:27 +0100 Subject: [PATCH] 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. --- cmd/apg/apg.go | 1 + config.go | 3 +++ random.go | 12 ++++++++++++ 3 files changed, 16 insertions(+) diff --git a/cmd/apg/apg.go b/cmd/apg/apg.go index d3382df..fca3457 100644 --- a/cmd/apg/apg.go +++ b/cmd/apg/apg.go @@ -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() diff --git a/config.go b/config.go index 1425a4c..3576aa9 100644 --- a/config.go +++ b/config.go @@ -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 diff --git a/random.go b/random.go index 4fa6df4..07036db 100644 --- a/random.go +++ b/random.go @@ -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() }