2024-03-12 20:59:07 +01:00
|
|
|
// SPDX-FileCopyrightText: 2021-2024 Winni Neessen <wn@neessen.dev>
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
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
|
2024-03-17 18:09:27 +01:00
|
|
|
// DefaultBinarySize is the default byte size for generating binary random bytes
|
|
|
|
DefaultBinarySize int64 = 32
|
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 {
|
2024-03-12 19:00:21 +01:00
|
|
|
// Algorithm sets the Algorithm used for the password generation
|
2023-08-04 16:02:58 +02:00
|
|
|
Algorithm Algorithm
|
2024-03-17 18:09:27 +01:00
|
|
|
// BinaryHexMode if set will output the hex representation of the generated
|
|
|
|
// binary random string
|
|
|
|
BinaryHexMode bool
|
|
|
|
// BinaryNewline if set will print out a new line in AlgoBinary mode
|
|
|
|
BinaryNewline bool
|
2024-03-12 19:00:21 +01:00
|
|
|
// CheckHIBP sets a flag if the generated password has to be checked
|
|
|
|
// against the HIBP pwned password database
|
|
|
|
CheckHIBP bool
|
2024-03-12 20:31:27 +01:00
|
|
|
// ExcludeChars is a list of characters that should be excluded from
|
|
|
|
// generated passwords
|
|
|
|
ExcludeChars string
|
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
|
2024-03-25 11:31:31 +01:00
|
|
|
// MobileGrouping indicates if the generated password should be grouped in a
|
|
|
|
// mobile-friendly manner
|
|
|
|
MobileGrouping bool
|
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
|
2024-03-08 16:03:02 +01:00
|
|
|
// SpellPassword if set will spell the generated passwords in the phonetic alphabet
|
|
|
|
SpellPassword bool
|
2024-03-12 18:28:01 +01:00
|
|
|
// SpellPronounceable if set will spell the generated pronounceable passwords in
|
|
|
|
// as its corresponding syllables
|
|
|
|
SpellPronounceable bool
|
2023-08-04 16:02:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2024-03-07 21:16:04 +01:00
|
|
|
func NewConfig(opts ...Option) *Config {
|
|
|
|
config := &Config{
|
2023-08-04 16:02:58 +02:00
|
|
|
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
|
2024-03-07 21:16:04 +01:00
|
|
|
for _, opt := range opts {
|
|
|
|
if opt == nil {
|
2023-08-04 16:02:58 +02:00
|
|
|
continue
|
|
|
|
}
|
2024-03-07 21:16:04 +01:00
|
|
|
opt(config)
|
|
|
|
}
|
|
|
|
return config
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithAlgorithm overrides the algorithm mode for the password generation
|
|
|
|
func WithAlgorithm(algo Algorithm) Option {
|
|
|
|
return func(config *Config) {
|
|
|
|
config.Algorithm = algo
|
2023-08-04 16:02:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:09:27 +01:00
|
|
|
// WithBinaryHexMode sets the hex mode for the AlgoBinary
|
|
|
|
func WithBinaryHexMode() Option {
|
|
|
|
return func(config *Config) {
|
|
|
|
config.BinaryHexMode = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-13 20:48:03 +01:00
|
|
|
// WithExcludeChars sets a list of characters to be excluded in the generated
|
|
|
|
// passwords
|
|
|
|
func WithExcludeChars(chars string) Option {
|
|
|
|
return func(config *Config) {
|
|
|
|
config.ExcludeChars = chars
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithFixedLength sets a fixed password length
|
|
|
|
func WithFixedLength(length int64) Option {
|
|
|
|
return func(config *Config) {
|
|
|
|
config.FixedLength = length
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-04 16:02:58 +02:00
|
|
|
// WithMinLength overrides the minimum password length
|
2024-03-07 21:16:04 +01:00
|
|
|
func WithMinLength(length int64) Option {
|
|
|
|
return func(config *Config) {
|
|
|
|
config.MinLength = length
|
2023-08-04 16:02:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-13 21:23:56 +01:00
|
|
|
// WithMinLowercase sets the minimum amount of lowercase characters that
|
|
|
|
// the generated password should contain
|
|
|
|
//
|
|
|
|
// CAVEAT: using to high values with this option, can lead to extraordinary
|
|
|
|
// calculation times, resulting in apg-go to never finish
|
|
|
|
func WithMinLowercase(amount int64) Option {
|
|
|
|
return func(config *Config) {
|
|
|
|
config.MinLowerCase = amount
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithMinNumeric sets the minimum amount of numeric characters that
|
|
|
|
// the generated password should contain
|
|
|
|
//
|
|
|
|
// CAVEAT: using to high values with this option, can lead to extraordinary
|
|
|
|
// calculation times, resulting in apg-go to never finish
|
|
|
|
func WithMinNumeric(amount int64) Option {
|
|
|
|
return func(config *Config) {
|
|
|
|
config.MinNumeric = amount
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithMinSpecial sets the minimum amount of special characters that
|
|
|
|
// the generated password should contain
|
|
|
|
//
|
|
|
|
// CAVEAT: using to high values with this option, can lead to extraordinary
|
|
|
|
// calculation times, resulting in apg-go to never finish
|
|
|
|
func WithMinSpecial(amount int64) Option {
|
|
|
|
return func(config *Config) {
|
|
|
|
config.MinSpecial = amount
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithMinUppercase sets the minimum amount of uppercase characters that
|
|
|
|
// the generated password should contain
|
|
|
|
//
|
|
|
|
// CAVEAT: using to high values with this option, can lead to extraordinary
|
|
|
|
// calculation times, resulting in apg-go to never finish
|
|
|
|
func WithMinUppercase(amount int64) Option {
|
|
|
|
return func(config *Config) {
|
|
|
|
config.MinUpperCase = amount
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-04 16:02:58 +02:00
|
|
|
// WithMaxLength overrides the maximum password length
|
2024-03-07 21:16:04 +01:00
|
|
|
func WithMaxLength(length int64) Option {
|
|
|
|
return func(config *Config) {
|
|
|
|
config.MaxLength = length
|
2023-08-04 16:02:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-25 11:31:31 +01:00
|
|
|
// WithMobileGrouping enables the mobile-friendly character grouping for AlgoRandom
|
|
|
|
func WithMobileGrouping() Option {
|
|
|
|
return func(config *Config) {
|
|
|
|
config.MobileGrouping = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-13 21:23:56 +01:00
|
|
|
// WithModeMask overrides the default mode mask for the random algorithm
|
|
|
|
func WithModeMask(mask ModeMask) Option {
|
2024-03-07 21:16:04 +01:00
|
|
|
return func(config *Config) {
|
2024-03-13 21:23:56 +01:00
|
|
|
config.Mode = mask
|
2023-08-04 16:02:58 +02:00
|
|
|
}
|
|
|
|
}
|