2023-08-04 16:02:58 +02:00
|
|
|
package apg
|
|
|
|
|
|
|
|
// List of default values for Config instances
|
|
|
|
const (
|
|
|
|
// DefaultMinLength reflects the default minimum length of a generated password
|
|
|
|
DefaultMinLength int64 = 12
|
|
|
|
// DefaultMaxLength reflects the default maximum length of a generated password
|
|
|
|
DefaultMaxLength int64 = 20
|
2023-08-05 14:47:30 +02:00
|
|
|
// DefaultMode sets the default character set mode bitmask to a combination of
|
|
|
|
// lower- and upper-case characters as well as numbers
|
2023-08-05 17:54:41 +02:00
|
|
|
DefaultMode ModeMask = ModeLowerCase | ModeNumeric | ModeUpperCase
|
2023-08-04 16:02:58 +02:00
|
|
|
// DefaultNumberPass reflects the default amount of passwords returned by the generator
|
|
|
|
DefaultNumberPass int64 = 6
|
|
|
|
)
|
|
|
|
|
|
|
|
// Config represents the apg.Generator config parameters
|
|
|
|
type Config struct {
|
|
|
|
// Algo
|
|
|
|
Algorithm Algorithm
|
2023-08-05 15:04:34 +02:00
|
|
|
// FixedLength sets a fixed length for generated passwords and ignores
|
|
|
|
// the MinLength and MaxLength values
|
|
|
|
FixedLength int64
|
2023-08-04 16:02:58 +02:00
|
|
|
// MaxLength sets the maximum length for a generated password
|
|
|
|
MaxLength int64
|
|
|
|
// MinLength sets the minimum length for a generated password
|
2023-08-05 17:57:06 +02:00
|
|
|
MinLength int64
|
|
|
|
// MinLowerCase represents the minimum amount of lower-case characters that have
|
|
|
|
// to be part of the generated password
|
2023-08-05 17:54:41 +02:00
|
|
|
MinLowerCase int64
|
2023-08-05 17:57:06 +02:00
|
|
|
// MinNumeric represents the minimum amount of numeric characters that have
|
|
|
|
// to be part of the generated password
|
|
|
|
MinNumeric int64
|
|
|
|
// MinSpecial represents the minimum amount of special characters that have
|
|
|
|
// to be part of the generated password
|
|
|
|
MinSpecial int64
|
|
|
|
// MinUpperCase represents the minimum amount of upper-case characters that have
|
|
|
|
// to be part of the generated password
|
2023-08-05 17:54:41 +02:00
|
|
|
MinUpperCase int64
|
2023-08-05 14:47:30 +02:00
|
|
|
// Mode holds the different character modes for the Random algorithm
|
|
|
|
Mode ModeMask
|
2023-08-04 16:02:58 +02:00
|
|
|
// NumberPass sets the number of passwords that are generated
|
|
|
|
// and returned by the generator
|
|
|
|
NumberPass int64
|
|
|
|
}
|
|
|
|
|
|
|
|
// Option is a function that can override default Config settings
|
|
|
|
type Option func(*Config)
|
|
|
|
|
|
|
|
// NewConfig creates a new Config instance and pre-fills it with sane
|
|
|
|
// default settings. The Config is returned as pointer value
|
|
|
|
func NewConfig(o ...Option) *Config {
|
|
|
|
c := &Config{
|
|
|
|
MaxLength: DefaultMaxLength,
|
|
|
|
MinLength: DefaultMinLength,
|
2023-08-05 14:47:30 +02:00
|
|
|
Mode: DefaultMode,
|
2023-08-04 16:02:58 +02:00
|
|
|
NumberPass: DefaultNumberPass,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Override defaults with optionally provided config.Option functions
|
|
|
|
for _, co := range o {
|
|
|
|
if co == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
co(c)
|
|
|
|
}
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithMinLength overrides the minimum password length
|
|
|
|
func WithMinLength(l int64) Option {
|
|
|
|
return func(c *Config) {
|
|
|
|
c.MinLength = l
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithMaxLength overrides the maximum password length
|
|
|
|
func WithMaxLength(l int64) Option {
|
|
|
|
return func(c *Config) {
|
|
|
|
c.MaxLength = l
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithNumberPass overrides the number of generated passwords setting
|
|
|
|
func WithNumberPass(n int64) Option {
|
|
|
|
return func(c *Config) {
|
|
|
|
c.NumberPass = n
|
|
|
|
}
|
|
|
|
}
|