mirror of
https://github.com/wneessen/apg-go.git
synced 2024-12-23 03:30:39 +01:00
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:
parent
2022e0953d
commit
2143ca99f5
3 changed files with 16 additions and 0 deletions
|
@ -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()
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
12
random.go
12
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()
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue